當使用intent的時候報出NullPoiterException,具體是在調用這行代碼的時候:
int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));
請多指教!謝謝
private void aggiungiImm(View arg1, int arg2) {
Intent i=newintent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
i.putExtra(Intent.EXTRA_TEXT, Integer.toString(arg2));
startActivityForResult(i, PICK_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
Uri contactData=null;
if (requestCode==PICK_REQUEST) {
if (resultCode==RESULT_OK) {
contactData=data.getData();
int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));
錯誤是不是和intent是隱式有關?
別用:
int posizione2 =Integer.parseInt((getIntent().getExtras().getString(Intent.EXTRA_TEXT)));
試試:
Bundle extras = getIntent().getExtras();
if (extras==null) {
Log.e( "", "No extras provided" );
return;
}
String myText = extras.getString(Intent.EXTRA_TEXT);
if (myText==null) {
Log.e( "", "No text provided" );
return;
}
int posizione2 = Integer.parseInt(myText);
可以看到是什麼引起異常。
返回的text在intent傳遞到函數會有效,所以還應該加上這行代碼:
int posizione2 = Integer.parseInt(data.getString(Intent.EXTRA_TEXT));
getIntent()
方法可以幫你啟動那個的Activity,