这是我的第一个应用程序,我搜索了几个关于该主题的帖子,但没有一个解决了我的问题...我创建了一个按钮,用于将变量(String)从一个活动发送到另一个活动,但按下它会导致应用程序崩溃......这是第一个活动的代码:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class PrimeraPregunta extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
final Button btSeisTres;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primera_pregunta);
btSeisTres = (Button)findViewById(R.id.btSeisTres);
btSeisTres.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String eleccionPuna = "6300";
Intent intent = new Intent(PrimeraPregunta.this, SegundaPregunta.class);
intent.putExtra("envioUno", eleccionPuna);
startActivity(intent);
}
});
}
}
这是第二项活动的代码:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SegundaPregunta extends AppCompatActivity {
TextView textViewName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_segunda_pregunta);
TextView textViewName = (TextView) findViewById(R.id.textViewName);
Intent intent = getIntent();
String eleccionPuna = intent.getExtras().getString("envioUno");
textViewName.setText(eleccionPuna);
}
}
最后,这是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxkuakxxgmail.knowyourchamp">
<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">
<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=".PrimeraPregunta" />
<activity android:name=".SegundaPregunta"/>
</application>
</manifest>
编辑:这是堆栈跟踪:
11-28 17:50:05.941 9837-9837/com.xxkuakxxgmail.knowyourchamp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxkuakxxgmail.knowyourchamp, PID: 9837
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxkuakxxgmail.knowyourchamp/com.xxkuakxxgmail.knowyourchamp.SegundaPregunta}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at com.xxkuakxxgmail.knowyourchamp.SegundaPregunta.onCreate(SegundaPregunta.java:17)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)