簡體   English   中英

不幸的是,應用程序已停止-在兩個活動之間傳遞Intent數據

[英]unfortunately App has Stopped- Passing Intent data between two Activites

我試圖使用從Authorites.class的意圖傳遞String數據到Issues.class。 選擇單選按鈕並單擊按鈕后,第一個活動必須顯示祝酒,並將單選按鈕值傳遞給下一個活動。 當我選擇單選按鈕並按Authorites.class中的按鈕時,該應用程序停止並退出。 請找出我哪里出問題了

 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:致命例外:主進程:com.example.chethan.wapp,PID:1960 java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.chethan.wapp / com.example.chethan.wapp.Issues}:位於android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)的java.lang.NullPointerException,位於android.app.ActivityThread.handleLaunchActivity( android.app.ActivityThread.access $ 800(ActivityThread.java:135)的ActivityThread.java:2233)android.os.Handler.dispatchMessageMes​​sage(Handler.android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) java:102)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5001)at java.lang.reflect.Method.invokeNative(本機方法)at 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:6 01)在dalvik.system.NativeStart.main(本機方法)造成原因:com.example.chethan.wapp.Issues.onCreate(Issues.java:34)處java.lang.NullPointerException android.app.Activity.performCreate( android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)上的Activity.java:5231)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)上android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)上的Activity.java:5231) )的android.app.ActivityThread.access $ 800(ActivityThread.java:135)的android.os.Handler.dispatchMessage(Handler.java:102)處的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)的情況下java.lang.reflect.Method.invokeNative(本機方法)處的android.app.ActivityThread.main(ActivityThread.java:5001)處的android.os.Looper.loop(Looper.java:136)。 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)的com.android.internal.os.ZygoteInit.main(ZygoteInit.j)的Method.invoke(Method.java:515) ava:601)在dalvik.system.NativeStart.main(本機方法)

因為您是從Issues類中的Intent的getString(“ Auth”)獲取字符串value1數據,但是在Authorities類中,所以您沒有在Intent obj中放置任何(“ Auth”)值。 所以取消注釋這條線...

           i.putExtra("Auth",auth);

然后運行項目...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM