Question

I am using Android API 17 with Android Development Tools (ADT) I have a Help buttton which is listening for click event. HelpButtonListener is listening for the click event for Help button as following:

Excerpt from MainActivity.java

private final class HelpButtonListener implements OnClickListener {
    public void onClick(View v) {           
        Intent mainToHelpIntent = new Intent(MainActivity.this, HelpActivity.class);
        startActivity(mainToHelpIntent);        
    }
}

Following code for HelpActivity works just fine ans shows me Some helpful stuff.. on HelpActivity creation:

public class HelpActivity extends RoboActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView helpTextView = new TextView(HelpActivity.this);
        helpTextView.setText("Some helpful stuff..");
        setContentView(helpTextView);
    }
}

But if I try to use set the text in the TextView view component injected using RoboGuice 2 for inject the TextView using the following code, it gives me a NullPointerException:

HelpActivity.java

public class HelpActivity extends RoboActivity {
    @InjectView(R.id.helpTextView)
    private TextView helpTextView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helpTextView.setText("Some helpful stuff..");
        setContentView(R.layout.activity_help);
    }
}

Following is the error I get when I click the Help button and try to set the text in the TextView created on activity_help.xml layout resource:

05-16 18:00:52.591: E/AndroidRuntime(9880): FATAL EXCEPTION: main
05-16 18:00:52.591: E/AndroidRuntime(9880): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wickedlynotsmart.myfirstapp/com.wickedlynotsmart.myfirstapp.activity.HelpActivity}: java.lang.NullPointerException
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.os.Looper.loop(Looper.java:137)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at java.lang.reflect.Method.invoke(Method.java:511)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at dalvik.system.NativeStart.main(Native Method)
05-16 18:00:52.591: E/AndroidRuntime(9880): Caused by: java.lang.NullPointerException
05-16 18:00:52.591: E/AndroidRuntime(9880):     at com.wickedlynotsmart.myfirstapp.activity.HelpActivity.onCreate(HelpActivity.java:22)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.Activity.performCreate(Activity.java:5104)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-16 18:00:52.591: E/AndroidRuntime(9880):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-16 18:00:52.591: E/AndroidRuntime(9880):     ... 11 more

Could someone help me understand why am I getting this error?

Thanks.

Was it helpful?

Solution

First set the content to the activity. Then initialize textview. Then set the text to textview. You have not initialized textview. Hence you got NullPointerException.

It should be like:

setContentView(R.layout.activity_help);
TextView helpTextView = (TextView)findViewById(R.id.helpTextView);
helpTextView.setText("Some helpful stuff..");

OTHER TIPS

// this is link

http://killertilapia.blogspot.in/2012/09/why-u-no-use-roboguice.html https://code.google.com/p/roboguice/wiki/SimpleExample

 public class HelpActivity extends RoboActivity {
 @InjectView(R.id.helpTextView)
 private TextView helpTextView;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help);
    helpTextView.setText("Some helpful stuff..");

  }
}

You set the content to the activity then initialize your textview by findViewById of the view hierarchy and then set text to textview. Your textview is not initialized hence you get NullPointerException.

private TextView helpTextView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_help); //set content tot eh activity
    helpTextView = (TextView) findViewById(R.id.textview); // initialize textview
    helpTextView.setText("Some helpful stuff.."); //set text to textview

}

According to this link https://code.google.com/p/roboguice/wiki/SimpleExample. Using Robospice could be as below.

private TextView helpTextView;
@InjectView(R.id.textview)             TextView helpTextView;   
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_help); //set content tot eh activity
    helpTextView.setText("Some helpful stuff.."); //set text to textview

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top