简体   繁体   中英

Android TextView null pointer exception

I really don't understand why my text-view is always null in the second activity. This are my files.

Error output:

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>

In 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);
}

you need to call setContentView() before you can use findViewById() .

Until the setContentView() has taken place, your view isn't in place, and so it can't be interrogated for what it contains. Move the last line up a bit, and it should solve the problem.

First you need to set the content view.

You are setting text to TextView before the TextView is Set.

Check the code below:

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM