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.