我目前正在尝试使用OptionsMenuItem
通过Intent
调用其他活动。我有2个布局。第一个布局有一个EditText
,其中包含从数据库中检索的字符串。第二个布局也有EditText
为空。 OptionsMenuItem
位于第一个布局中。每当我点击MenuItem
时,它应该调出第二个布局并将第一个布局中的文本提取到第二个布局中的EditText
,但它会崩溃。我尝试使用ActivityResult方式,但我仍然无法让它工作。希望你们能帮助我。
这是我的java代码,其意图是第一个布局的主要活动:
@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);
}
// 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;
}
}
}
这是我的第二个布局的java代码:
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();
}
}
我的logcat由srinathhs请求: 11-22 04:39:12.635:ERROR / AndroidRuntime(456):未捕获的处理程序:由于未捕获的异常导致主线程退出 11-22 04:39:12.815:ERROR / AndroidRuntime(456):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.notepad / com.example.android.notepad.TitleEditor}:java.lang 。空指针异常 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.app.ActivityThread.access $ 2100(ActivityThread.java:116) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.os.Handler.dispatchMessage(Handler.java:99) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.os.Looper.loop(Looper.java:123) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在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):引起:java.lang.NullPointerException 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.content.ContentResolver.acquireProvider(ContentResolver.java:574) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.content.ContentResolver.query(ContentResolver.java:147) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 11-22 04:39:12.815:ERROR / AndroidRuntime(456):... 11更多
答案 0 :(得分:3)
快速提问,您是否在清单中注册了第二项活动?如果没有,那么每次调用它时都会使应用程序崩溃。
答案 1 :(得分:0)
这个问题已经结束。我无意中找到了解决此错误的答案。似乎我的代码的两行干扰了创建第二个活动。谢谢大家^^