startActivityForResult()按钮崩溃

时间:2019-04-14 15:30:10

标签: java android android-activity hyperlink

我试图用一个按钮链接2个活动(MainActivity和Taskcreation)。我期望第二个活动产生结果,所以我正在使用startActivityForResult(),但是在开始主要活动时,它会继续崩溃。

没有按钮和链接的情况下,主活动正常运行。我访问的每个关于startActivityForResult的论坛都指出问题是关于放置MainActivity.this(上下文)而不是仅此而已,但这就是我从一开始就一直在做的事情。显然,单独使用它也不行。

MainActivity.java按钮创建/链接:

private static final int REQUEST_ADD = 1;


    Button AjoutTache = (Button) findViewById(R.id.AjoutTache);
    AjoutTache.setOnClickListener( new OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this, Taskcreation.class);
            startActivityForResult(intent, REQUEST_ADD);
            //startActivityForResult(intent, 1);
        }
    });

Taskcreation.java结果:

  buttonOk.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent result = new Intent();
            setResult(RESULT_OK, result);
            result.putExtra("nom", nomTache.getText().toString());
            result.putExtra("statut", statut.isChecked());
            result.putExtra("priorite", ((RadioButton) findViewById(priorite.getCheckedRadioButtonId())).getText());
            result.putExtra("deadline", deadline.getText().toString());
            finish();
        }
    });

MainActivity.java onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == REQUEST_ADD) {
        if (resultCode == RESULT_OK) {
            // Create Task
            Tache tache = new Tache();
            tache.setNom(data.getStringExtra("nom"));
            tache.setStatut(data.getBooleanExtra("statut", false));
            tache.setPriorite(data.getStringExtra("priorite"));
            tache.setDeadline(data.getStringExtra("deadline"));
            //mesTaches.add(tache);
            // Toast
            Toast.makeText(this, "Task added:\n" + tache.toString(), Toast.LENGTH_LONG).show();
            // Update listview
        }
        else if (resultCode == RESULT_CANCELED) {
            Log.d("Main", "canceled");
        }
    }
}

当我在虚拟设备中启动应用程序时,它只是崩溃了。

这是最新的堆栈跟踪:

2019-04-14 17:44:13.694 6009-12813 /? E / ctxmgr: [SyncServerInterestRecordsOperation] WriteInterestRecord失败:网络状态= -1 [CONTEXT service_id = 47]

com.android.volley.VolleyError: Unable to obtain auth token - is the device online?

    at eme.a(:com.google.android.gms@16089040@16.0.89 (100700-239467275):31)

    at eha.run(:com.google.android.gms@16089040@16.0.89 (100700-239467275):2)

    at egy.handleMessage(:com.google.android.gms@16089040@16.0.89 (100700-239467275):3)

    at rlu.run(:com.google.android.gms@16089040@16.0.89 (100700-239467275):8)

    at rmf.b(:com.google.android.gms@16089040@16.0.89 (100700-239467275):32)

    at rmf.run(:com.google.android.gms@16089040@16.0.89 (100700-239467275):21)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

    at rsc.run(Unknown Source:7)

    at java.lang.Thread.run(Thread.java:764)

1 个答案:

答案 0 :(得分:0)

########### intent.settype(“”);

use this
public void onClick(View v){
            Intent intent = new Intent(MainActivity.this, Taskcreation.class);

            intent.settype("");

            startActivityForResult(intent, REQUEST_ADD);
            //startActivityForResult(intent, 1);
        }
    });