我正在使用带有 Android 开发工具 (ADT) 的 Android API 17 我有一个帮助按钮,它正在监听点击事件。HelpButtonListener正在监听帮助按钮的点击事件,如下所示:
摘自 MainActivity.java
private final class HelpButtonListener implements OnClickListener {
public void onClick(View v) {
Intent mainToHelpIntent = new Intent(MainActivity.this, HelpActivity.class);
startActivity(mainToHelpIntent);
}
}
HelpActivity的以下代码工作得很好,并向我展示了一些有用的东西..关于HelpActivity
创建:
public class HelpActivity extends RoboActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView helpTextView = new TextView(HelpActivity.this);
helpTextView.setText("Some helpful stuff..");
setContentView(helpTextView);
}
}
但是,如果我尝试在TextView
使用 RoboGuice 2 注入的视图组件中设置文本以TextView
使用以下代码注入,它会给我一个NullPointerException
:
帮助活动.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);
}
}
以下是我单击帮助按钮并尝试在TextView
创建的activity_help.xml
布局资源中设置文本时遇到的错误:
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
有人可以帮我理解为什么我会收到这个错误吗?
谢谢。