我正在尝试使用Authorites.class中的intent传递String数据到Issues.class。选择单选按钮并单击按钮时,第一个活动必须显示吐司,并将单选按钮值传递给下一个活动。 当我选择单选按钮并按下Authorites.class中的按钮时,应用程序将停止并退出。 Plz找到我哪里出错了
public class Authorities extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
//private GoogleApiClient client;
RadioButton auth_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authorities);
final RadioGroup auth_grp = (RadioGroup) findViewById(R.id.rgrp);
Button button1 = (Button) findViewById(R.id.authselect);
button1.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick (View v){
// store the text corresponding to the RadioButton which is clicke
int sa = auth_grp.getCheckedRadioButtonId();
auth_button = (RadioButton) findViewById(sa);
// String auth=auth_button.getText().toString();
Toast.makeText(Authorities.this, auth_button.getText(), Toast.LENGTH_SHORT).show();
Intent i;
i=new Intent(Authorities.this, Issues.class);
// Intent i = new Intent().setClassName("com.example.chethan.wapp.Authorities", "com.example.chethan.wapp.Issues");
// i.putExtra("Auth",auth);
// Starts TargetActivity
// Authorities.this.startActivity(i);
startActivity(i);
}
});
}
Issues.class应该接收从先前活动返回的Intent数据
public class Issues extends AppCompatActivity {
CheckBox mws,sl,swc,sws,dr;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// mws=(CheckBox)findViewById(R.id.mws);
// sl=(CheckBox)findViewById(R.id.sl);
// swc=(CheckBox)findViewById(R.id.swc);
// sws=(CheckBox)findViewById(R.id.sws);
// dr=(CheckBox)findViewById(R.id.dr);
b2=(Button)findViewById(R.id.nxt);
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Auth");
Toast.makeText(getApplicationContext(),"Authority you chose is:\n"+value1,Toast.LENGTH_LONG).show();
}
这是日志猫:
02-29 10:54:00.594 1960-1960 / com.example.chethan.wapp E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.example.chethan.wapp,PID:1960 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.chethan.wapp / com.example.chethan.wapp.Issues}:java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException 在com.example.chethan.wapp.Issues.onCreate(Issues.java:34) 在android.app.Activity.performCreate(Activity.java:5231) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
因为你从问题类的Intent的getString(" Auth")中获取字符串value1数据,但是在Authorities类中你没有在Intent obj中输入任何(" Auth")值。 所以取消注释这条线......
i.putExtra("Auth",auth);
然后运行项目......