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.