-2

There is my Take all my time when I need to hurry problem..

I just make an intent to travel informations from my main to an other activity. But the data don't go to the other activity.

Pick an eye just some second please.

MainActivity.class

public class MainActivity extends AppCompatActivity {

public static final String KEY = "KEY";
Button button;

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

    button = findViewById(R.id.button);

    button.setOnClickListener(v -> {

        Intent intent = new Intent(MainActivity.this, OtherActivity.class);

        intent.putExtra(KEY, "FONCTIONNE");

        startActivity(intent);

        });
    }
}

OtherActivity.class

import static com.example.marguerite.experiences.MainActivity.KEY;

public class OtherActivity extends AppCompatActivity {

Button button;
TextView textView;
String text;

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

        button = findViewById(R.id.button);
        textView = findViewById(R.id.textview);

        text = getIntent().getStringExtra(KEY);

        textView.setText(text);

        button.setOnClickListener(v ->finish());
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marguerite.experiences">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:fullBackupContent="@xml/backup_descriptor">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".OtherActivity" />
</application>

</manifest>

ERROR EVENT

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.marguerite.experiences, PID: 8496
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marguerite.experiences/com.example.marguerite.experiences.OtherActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at com.example.marguerite.experiences.OtherActivity.onCreate(OtherActivity.java:26)
                  at android.app.Activity.performCreate(Activity.java:7136)

There it is. I certainly forgot something but all tutorials I have looked about use the same easy technique.

user8886048
  • 109
  • 6

2 Answers2

1

The following line...

textView.setText(text);

... provokes the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

which means that textView is null. There is no view with id textview in activity_other.xml. Make sure that the view exists and rule out any typos.

See also: What is a NullPointerException, and how do I fix it?

Markus Penguin
  • 1,581
  • 12
  • 19
  • Yeah, thanks you for your help. I need this to ensure my self this was the good technic, i work on an other application and use the same but it would'nt work. I copy/past this one once the code was corrected and it worked so thank you ! The problem of the other wasn't a missing id, i'll never know tho ... –  Sep 28 '18 at 13:43
0

All the above answers should work but if still not working try changing

 button = findViewById(R.id.button);
    textView = findViewById(R.id.textview);  

to

 button = (Button)findViewById(R.id.button);
    textView = (TextView)findViewById(R.id.textview);  

in both activities

and when you get the intent in OtherActivity call it like

text = getIntent.getStringExtra("KEY");
Kevin Kurien
  • 812
  • 6
  • 14