0

I'm new to Android development and I've just tried to create a simple app that gets input and returns a result in a new activity based on input. I'm getting a NullPointerException once I call the new activity. Help would be greatly appreciated! Thanks in advance.

LogCat:

11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libEGL_adreno200.so
11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-29 19:03:47.369: D/libEGL(26311): loaded /system/lib/egl/libGLESv2_adreno200.so
11-29 19:03:47.369: I/Adreno200-EGL(26311): <qeglDrvAPI_eglInitialize:265>: EGL 1.4        
QUALCOMM Build: Iabe52cfaeae4c5fab1acacfe6f056ba15fa93274
11-29 19:03:47.409: D/OpenGLRenderer(26311): Enabling debug mode 0
11-29 19:03:48.430: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans     
cannot have a zero length
11-29 19:03:48.430: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans      
cannot have a zero length
11-29 19:03:48.450: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans 
cannot have a zero length
11-29 19:03:48.450: E/SpannableStringBuilder(26311): SPAN_EXCLUSIVE_EXCLUSIVE spans 
cannot have a zero length
11-29 19:03:51.693: W/IInputConnectionWrapper(26311): beginBatchEdit on inactive 
InputConnection
11-29 19:03:51.693: W/IInputConnectionWrapper(26311): endBatchEdit on inactive 
InputConnection
11-29 19:03:54.606: D/AndroidRuntime(26311): Shutting down VM
11-29 19:03:54.606: W/dalvikvm(26311): threadid=1: thread exiting with uncaught 
exception (group=0x416697c0)
11-29 19:03:54.606: E/AndroidRuntime(26311): FATAL EXCEPTION: main
11-29 19:03:54.606: E/AndroidRuntime(26311): java.lang.RuntimeException: Unable to 
start activity ComponentInfo{com.example.newbloodexam/
com.example.newbloodexam.DisplayResultsActivity}: java.lang.NullPointerException
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at   
android.app.ActivityThread.access$600(ActivityThread.java:142)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.os.Handler.dispatchMessage(Handler.java:99)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at  
android.os.Looper.loop(Looper.java:137)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.ActivityThread.main(ActivityThread.java:5104)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
java.lang.reflect.Method.invokeNative(Native Method)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
java.lang.reflect.Method.invoke(Method.java:525)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
dalvik.system.NativeStart.main(Native Method)
11-29 19:03:54.606: E/AndroidRuntime(26311): Caused by: java.lang.NullPointerException
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
com.example.newbloodexam.DisplayResultsActivity.onCreate
(DisplayResultsActivity.java:36)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.Activity.performCreate(Activity.java:5133)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-29 19:03:54.606: E/AndroidRuntime(26311):    ... 11 more
11-29 19:03:56.478: I/Process(26311): Sending signal. PID: 26311 SIG: 9

MainActivity:

package com.example.newbloodexam;

import com.example.newbloodexam.DisplayResultsActivity;
import com.example.newbloodexam.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void CalcResult(View view)
{

    String NameExtra = "NameStr"; String AgeExtra = "AgeInt"; String TestExtra = "TestInt";
    Intent intent = new Intent(this, DisplayResultsActivity.class);
    EditText NameEdit = (EditText) findViewById(R.id.EnterName);
    EditText AgeEdit  = (EditText) findViewById(R.id.EnterAge);
    EditText TestEdit = (EditText) findViewById(R.id.EnterTest);
    String NameStr = NameEdit.getText().toString();
    int AgeInt  = Integer.parseInt(AgeEdit.getText().toString());
    int TestInt = Integer.parseInt(TestEdit.getText().toString());
    Bundle ExtraBundle = new Bundle();
    ExtraBundle.putString(NameExtra, NameStr);
    ExtraBundle.putInt(AgeExtra, AgeInt);
    ExtraBundle.putInt(TestExtra, TestInt);
    intent.putExtras(ExtraBundle);
    startActivity(intent);


     }

}

DisplayResultsActivity:

package com.example.newbloodexam;

import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class DisplayResultsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_display_results);
    // Show the Up button in the action bar.
    setupActionBar();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    Intent intent = getIntent();
    Bundle Extra = intent.getExtras();
    String Name = Extra.getString("NameStr");
    int Age  = Extra.getInt("AgeInt");
    int Test = Extra.getInt("TestInt");
    String Result = Calculate(Name,Age,Test);
    //TextView textView = new TextView(this);
    //textView.setTextSize(40);
    //textView.setText(Result);
    //setContentView(textView);
    TextView ResultVal = (TextView) findViewById(R.id.textView3);
    ResultVal.setText(Result);
    setContentView(ResultVal);

}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_results, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-     
 vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public String Calculate(String getName, int getAge, int getTest)
{
    String ResultText = "";
    if(getAge < 18)
    {
        if(getTest > 0 && getTest <= 100)
        {
            ResultText = "Your ok.";
        }
        else if(getTest > 100)
        {
            ResultText = "wtf?";
        }
    }
    else if(getAge < 18 && getAge <= 50)
    {
        if(getTest > 0 && getTest >= 500)
        {
            ResultText = "Your a healthy one";
        }
        else if(getTest > 500)
        {
            ResultText = "Your gonna die.";
        }
    }

    return ResultText;
}

}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbloodexam"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.newbloodexam.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.newbloodexam.DisplayResultsActivity"
        android:label="@string/title_activity_display_results" >
    </activity>
</application>

</manifest>

3 Answers 3

1

It seems the problem is in:

TextView ResultVal = (TextView) findViewById(R.id.textView3);
ResultVal.setText(Result);
setContentView(ResultVal);  

ResultVal is null.
You can´t use findViewById before setContentView

Sign up to request clarification or add additional context in comments.

Comments

0

In your DisplayResultsActivity you need to call setContentView(ResultVal) before attempting to findViewById(R.id.textView3).

The call to findViewById(R.id.textView3) is returning null because the view is not inflated until the call to setContentView(ResultVal) is made.

The following lines point right to the line of code causing the crash:

11-29 19:03:54.606: E/AndroidRuntime(26311): Caused by: java.lang.NullPointerException
11-29 19:03:54.606: E/AndroidRuntime(26311):    at 
com.example.newbloodexam.DisplayResultsActivity.onCreate
(DisplayResultsActivity.java:36)

Line 36 in the DisplayResultsActivity.java is what is causing the crash.

Comments

0

setContentView() has to be the very second line after super.onCreate(savedInstanceState) (good programming practise) Your resulVal is throwing nullPointerexception because the view is not inflated until the call to setContentView(ResultVal) is made.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.