Intent crashes my app
OptionsMenuItem
using Intent
. I have 2 layouts. The 1st layout has a EditText
with a string that is retrieved from a database. The 2nd layout also have a EditText
which is empty. The OptionsMenuItem
is in the 1st layout. Whenever I click on the MenuItem
, it is supposed to call up the 2nd layout and extract the text from the 1st layout into the EditText
in the 2nd layout, but instead it crashes. I tried using the ActivityResult way but I still can't get it to work. Hope you guys can help me out.
This is my java code where the intent is for the main activity with the 1st layout:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Build the menus that are shown when editing.
if (mState == STATE_EDIT) {
menu.add(0, REVERT_ID, 0, R.string.menu_revert)
.setIcon(android.R.drawable.ic_menu_revert);
if (!mNoteOnly) {
menu.add(0, DELETE_ID, 0, R.string.menu_delete)
.setIcon(android.R.drawable.ic_menu_delete);
menu.add(1, ADD_ID, 1, R.string.add_namespace);
menu.add(2, MENU_ITEM_ADD, 2, R.string.menu_add)开发者_JS百科;
}
// Build the menus that are shown when inserting.
} else {
menu.add(0, DISCARD_ID, 0, R.string.menu_discard)
.setIcon(android.R.drawable.ic_menu_delete);
menu.add(1, ADD_ID, 1, R.string.add_namespace);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle all of the possible menu actions.
switch (item.getItemId()) {
case DELETE_ID:
deleteNote();
finish();
break;
case DISCARD_ID:
cancelNote();
break;
case REVERT_ID:
cancelNote();
break;
case ADD_ID:
addNamespace();
break;
case MENU_ITEM_ADD:
testActivity();
break;
}
return super.onOptionsItemSelected(item);
}
private final void testActivity()
{
Intent i = new Intent(this, TitleEditor.class);
startActivityForResult(i, STATIC_INTEGER_VALUE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case(STATIC_INTEGER_VALUE):
{
if(resultCode == Activity.RESULT_OK)
{
newTextTemplate = (EditText) findViewById(R.id.smsTemplate);
String newTemplate = data.getStringExtra(TitleEditor.PUBLIC_STATIC_STRING_IDENTIFIER);
newTextTemplate.setText(newTemplate);
}
break;
}
}
}
This is my java code with the 2nd layout:
public class TitleEditor extends Activity implements View.OnClickListener {
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID, // 0
NotePad.Notes.TITLE, // 1
NotePad.Notes.TEMPLATE, // 2
};
private static final int COLUMN_INDEX_TITLE = 1;
private static final int COLUMN_INDEX_TEMPLATE = 2;
private Cursor mCursor;
private EditText mToContacts;
private EditText mFromTemplate;
public static final String PUBLIC_STATIC_STRING_IDENTIFIER = null;
private Uri mUri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_editor);
mUri = getIntent().getData();
mCursor = managedQuery(mUri, PROJECTION, null, null, null);
mFromTemplate = (EditText) this.findViewById(R.id.smsTemplate);
mFromTemplate.setOnClickListener(this);
Button b = (Button) findViewById(R.id.ok);
b.setOnClickListener(this);
}
public void onClick(View v) {
Intent resultIntent = new Intent();
resultIntent.putExtra(PUBLIC_STATIC_STRING_IDENTIFIER, NoteEditor.smsTemplate);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
My logcat requested by srinathhs: 11-22 04:39:12.635: ERROR/AndroidRuntime(456): Uncaught handler: thread main exiting due to uncaught exception 11-22 04:39:12.815: ERROR/AndroidRuntime(456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.notepad/com.example.android.notepad.TitleEditor}: java.lang.NullPointerException 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread.access$2100(ActivityThread.java:116) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.os.Handler.dispatchMessage(Handler.java:99) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.os.Looper.loop(Looper.java:123) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread.main(ActivityThread.java:4203) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at java.lang.reflect.Method.invokeNative(Native Method) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at java.lang.reflect.Method.invoke(Method.java:521) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at dalvik.system.NativeStart.main(Native Method) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): Caused by: java.lang.NullPointerException 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.content.ContentResolver.acquireProvider(ContentResolver.java:574) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.content.ContentResolver.query(ContentResolver.java:147) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.Activity.managedQuery(Activity.java:1493) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at com.example.android.notepad.TitleEditor.onCreate(TitleEditor.java:84) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 11-22 04:39:12.815: ERROR/AndroidRuntime(456): ... 11 more
Quick question, did you register the 2nd activity within your manifest? If not, that'll crash the application every time it would be invoked.
This question is closed. I accidentally found the answer to solve this error. It seems that 2 lines of my code is interfering with creating the 2nd activity. Thanks people ^^
精彩评论