为什么会导致NullPointerException?

时间:2013-08-16 05:29:23

标签: java android

我有一个如下程序,任何人都可以告诉我获得NPE的原因是什么。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        secondClass x = new secondClass();
        x.sText("Test Spring");

    }

}



public class secondClass extends MainActivity {

    public void sText(String s) {

        TextView text = (TextView) findViewById(R.id.text);
        text.setText(s);
    }

}

Mainfest

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ...

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:id="@+id/text"
     />

</RelativeLayout>

logcat的

08-16 01:31:11.320  15032-15032/? E/Trace: error opening trace file: No such file or directory (2)
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: App crashed! Process: com.example.myapplication
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: App crashed! Package: com.example.myapplication v1 (1.0)
08-16 01:31:11.420      836-987/? E/EmbeddedLogger: Application Label: My Application
08-16 01:31:11.420  15032-15032/? E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
        at android.app.ActivityThread.access$600(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:110)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java)
        at com.example.myapplication.secondClass.sText(secondClass.java:13)
        at com.example.myapplication.MainActivity.onCreate(MainActivity.java:14)
        at android.app.Activity.performCreate(Activity.java)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)

据我所知,这应该实例化并初始化secondClass对象,然后调用它的sText()方法。然后,sText方法应该在TextView中存储引用。接下来,它调用TextView上的setText()方法,该方法将TextView中的文本更改为传递给sText()的文本。

logcat说它抛出了NullPointerException,但我不完全确定原因。如果这是一个新手的错误,那是因为我是一个新秀。谢谢。

1 个答案:

答案 0 :(得分:3)

您需要在secondActivity中覆盖onCreate并拥有setContentView(R.layout.mylayout)。然后,您可以初始化文本视图。

您还需要使用意图

启动活动
     Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
     startActivity(intent);

请记住在清单文件

中为活动(SecondActivtiy)创建一个条目

如果需要将字符串传递给第二个活动,请使用intent.putExtra

      Intent intent = new Intent(MainActivtiy.this,SecondActivity.class);
      intent.putExtra("key",mystring); 
      startActivity(intent);

然后在secondActivity

    TextView tv ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mysecondlayout);
    tv = (TextView) findViewByid(R.id.textView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
    String value = extras.getString("key");
    tv.setText(value);
    //get the value based on the key
    }

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String,android.os.Bundle)

您可以findViewById当前视图层次设置为活动。

此外,您需要使用intent而不是执行此econdClass x = new secondClass()

来启动活动