Android TextView空指针异常

时间:2015-11-09 13:37:19

标签: java android xml android-layout android-intent

我真的不明白为什么我的文本视图在第二个活动中始终为空。 这是我的档案。

错误输出:

11-09 08:23:24.708 9269-9269/com.example.andrei.lifeciycle E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-09 08:23:24.708 9269-9269/com.example.andrei.lifeciycle E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
11-09 08:23:24.708 9269-9269/com.example.andrei.lifeciycle E/AndroidRuntime:     at com.example.andrei.lifeciycle.ShowSecondActivity.onCreate(ShowSecondActivity.java:26)
11-09 08:23:24.708 9269-9269/com.example.andrei.lifeciycle E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990)
11-09 08:23:24.708 9269-9269/com.example.andrei.lifeciycle E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)

MainAcivity.class

public class MainActivity extends AppCompatActivity {

    private static final String TAG_LOG = MainActivity.class.getSimpleName() ;
    public static final String EXTRA_MESSAGE = "com.example.andrei.lifeciycle" ;
    private EditText editText;
    private String message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Log.d(TAG_LOG, "am pornit onCreate");
    }

    public void send_message(View view){

        editText = (EditText) findViewById(R.id.edit_text);
        message = editText.getText().toString();
        Intent intent = new Intent(this, ShowSecondActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

ShowSecondActivity.class

public class ShowSecondActivity extends AppCompatActivity {

    private static final String TAG_LOG = ShowSecondActivity.class.getSimpleName() ;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        textView = (TextView)findViewById(R.id.textViewdata);
        textView.setText(message);

        setContentView(R.layout.activity_show_second);
    }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_weight="3"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_text"
        android:hint="Hello World!"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/button"
        android:layout_weight="1"
        android:onClick="send_message"/>
</LinearLayout>

activity_show_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_show_second"
    tools:context="com.example.andrei.lifeciycle.ShowSecondActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewdata" />
</LinearLayout>

2 个答案:

答案 0 :(得分:4)

ShowSecondActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    textView = (TextView)findViewById(R.id.textViewdata);
    textView.setText(message);

    setContentView(R.layout.activity_show_second);
}

您必须先致电setContentView(),然后才能使用findViewById()

setContentView()发生之前,您的观点尚未到位,因此无法查询其中包含的内容。将最后一行向上移动一点,它应该可以解决问题。

答案 1 :(得分:1)

首先,您需要设置内容视图。

  

在设置TextView之前,您要将文本设置为TextView。

请检查以下代码:

public class ShowSecondActivity extends AppCompatActivity {

private static final String TAG_LOG = ShowSecondActivity.class.getSimpleName() ;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_show_second);

    Intent intent = getIntent();
    String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    textView = (TextView)findViewById(R.id.textViewdata);
    textView.setText(message);


}