OnClickListener在startActivity上抛出NullPointerException

时间:2013-05-18 15:43:03

标签: java android android-intent android-activity roboguice

我正在使用Android API 17和Android开发工具(ADT)我有一个帮助按钮,它正在侦听点击事件。 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 的代码可以很好地向我展示{strong>一些有用的东西.. 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);
    }
}

但是,如果我尝试使用RoboGuice 2注入TextView视图组件中的文本,使用以下代码注入TextView,它会给我一个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);
    }
}

以下是我单击“帮助”按钮并尝试在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

有人可以帮我理解为什么会收到此错误?

感谢。

3 个答案:

答案 0 :(得分:3)

首先将内容设置为活动。然后初始化textview。然后将文本设置为textview。您尚未初始化textview。因此你得到了NullPointerException。

应该是这样的:

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

答案 1 :(得分:1)

//这是链接

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..");

  }
}

答案 2 :(得分:1)

您可以将内容设置为活动,然后通过视图层次结构的findViewById初始化textview,然后将文本设置为textview。您的textview未初始化,因此您获得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

}

根据此链接https://code.google.com/p/roboguice/wiki/SimpleExample。使用Robospice可以如下。

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

}