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.
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);
});
}
}
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());
}
}
<?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>
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.
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?
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");
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.