0

我有以下内部活动的活动

public class Closer extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        doMyWork();
    }

    public void gotoCat() {
        Intent intent = new Intent(this, CatActivity.class);
        startActivityForResult(intent, 1);
    }
}

但是当我运行代码时,我得到了一个根植于该行的 NPE startActivityForResult(intent, 1)。我将如何成功地做到这一点?

来自封闭活动的调用是new Closer.gotoCat()

错误:

  java.lang.NullPointerException
at android.app.Activity.startActivityForResult(Activity.java:3430)
at android.app.Activity.startActivityForResult(Activity.java:3391)
at com.company.Game.widget.MyView$Closer.gotoCat(MyView.java:496)
at com.company.Game.widget.MyView.onDragEndSoCallCat(MyView.java:315)
at com.company.Game.widget.MyView.onTouch(MyView.java:254)
at android.view.View.dispatchTouchEvent(View.java:7456)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2264)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1995)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
4

1 回答 1

1

You can call gotoCat() method like this :- 1. Write down following code in your Closer Activity class

public class Closer extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gotoCat();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == 1) {

             if(resultCode == RESULT_OK){      
                 String result=data.getStringExtra("result");  
                 doMyWork(result);
             }
             if (resultCode == RESULT_CANCELED) {    
                 //Write your code if there's no result
             }
          }
}

public void gotoCat() {
    Intent intent = new Intent(this, CatActivity.class);
    startActivityForResult(intent, 1);
}
public void doMyWork(String result) {
    // TODO Auto-generated method stub
    Log.d("hello", "Saurabh"+result);
    Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();    
}
}

Then in put following code in your second Activity like this:-

public class CatActivity extends Activity{
    public String result = "Coming Back from Second Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

         Intent returnIntent = new Intent(getBaseContext(), MainActivity.class);
         returnIntent.putExtra("result",result);
         setResult(RESULT_OK,returnIntent);     
         finish();  
    }
}

It is working Fine. Hope it will help you.

于 2013-10-16T07:03:28.457 回答