0

我正在使用带有 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

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

谢谢。

4

3 回答 3

3

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

它应该是这样的:

setContentView(R.layout.activity_help);
TextView helpTextView = (TextView)findViewById(R.id.helpTextView);
helpTextView.setText("Some helpful stuff..");
于 2013-05-18T15:54:50.763 回答
1

您将内容设置为活动,然后通过视图层次结构的 findViewById 初始化您的 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

}
于 2013-05-18T16:12:18.167 回答
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..");

  }
}
于 2013-05-18T16:03:23.270 回答