简体   繁体   English

onActivityResult()不被调用?

[英]onActivityResult() Does not get called?

I have an MainActivity which has two TextView . 我有一个MainActivity有两个TextView User has an option to start another activity and choose data from ListView to fill those TextView . 用户可以选择启动另一个活动,并从ListView选择数据以填充这些TextView My main activity has an OnClickListener which starts an Activty from which user can select data and come back to main activity.My OnClickListener code looks likes this: 我的主要活动有一个OnClickListener ,它启动一个Activty ,用户可以从中选择数据并返回到主要活动。我的OnClickListener代码如下所示:

private static final int PICK_START = 0;
private static final int PICK_END = 1;
@Override
    public void onClick(View v) {
        Log.i(MainActivity, "view clicked");
        int id = v.getId();

        if (id == R.id.searchR) {
                        //do nothing
        } else if (id == R.id.startSearch) {
            Intent startIntent = new Intent(this, SList.class);
            startActivityForResult(startIntent, PICK_START);

        } else if (id == R.id.endSearch) {
            Intent startIntent = new Intent(this, SList.class);
            startActivityForResult(startIntent, PICK_END);
        }

    }

When the above onClick method gets called and after that its starts another activity SList.class .In that I have a listview from which user can select the value and upon selecting value the result will be set and activity will finish itself.Code for this is: 当上面的onClick方法被调用并随后启动另一个活动SList.class时 ,我有一个列表视图,用户可以从中选择值,选择值后将设置结果,活动将自行完成。 :

sListview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Station selectedS = sArray.get(position);
                Intent returnIntent = new Intent();
                returnIntent.putExtra("_id", selectedS.getId());
                returnIntent.putExtra("name", selectedS.getName());
                setResult(RESULT_OK, returnIntent);
                finish();
            }
        });

In above code activity sets the result and finishes itself.Till here everything is working accordingly.But after that when the previous activity is started, the onActivityResult() method nevers gets called.The code for onActivityResult() is : 在上面的代码中设置活动的结果,并完成itself.Till这里的一切后工作accordingly.But当以前的活动开始后, onActivityResult()方法得到讷韦尔为called.The代码onActivityResult()是:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(MainActivity, "" + requestCode);

        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Check which request we're responding to
            if (requestCode == PICK_START) {
                //data.getStringExtra("_id");
                Log.i(MainActivity, data.getStringExtra("name"));

            } else if (requestCode == PICK_END) {
                Log.i(MainActivity, data.getStringExtra("name"));
            }
        }
    }

I dont know why onActivityResult is never triggered .Someone even wrote on his blog that There is bug in android API. 我不知道为什么onActivityResult永远不会触发。甚至有人在他的博客上写道,Android API中存在错误。 In startActivityForResult(intent, requestCode); startActivityForResult(intent, requestCode); This function does work as long as requestCode = 0 . 只要requestCode = 0此函数就起作用。 However, if you change the request code to anything other than zero, the ApiDemos will fail (and onActivityResult() won't be called). 但是,如果将请求代码更改为零以外的任何值,则ApiDemos将失败(并且不会调用onActivityResult())。

我找到了解决问题的方法,只需要重新启动Eclipse,代码就可以开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM