我想我应该展示整个布局结构,以便您找出问题所在。我是 Android 编程新手,不确定我在做什么是正确的。我用 listview 加载了 main.xml。然后我有一个菜单,当按下菜单时,我加载 add.xml 以将新列表添加到列表视图。它如下,工作正常。
final View addView = getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView)
.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.IDeditText));
}
}).setNegativeButton("Cancel", null).show();
add.xml有一个 EditText 视图。当用户点击 EditText 时,我想加载另一个名为 description.xml 的 XML 布局。这样做如下 -
private void add() {
final View addView = getLayoutInflater().inflate(R.layout.add, null);
EditText DescEditTxt = (EditText) addView.findViewById(R.id.descriptionEdit);
if(DescEditTxt != null)
{
DescEditTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
final View descriptionView = getLayoutInflater().inflate(R.layout.description, null);
setContentView(descriptionView);
}
});
}
new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView)
.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.IDeditText));
}
}).setNegativeButton("Cancel", null).show();
}
当用户关注 EditText 视图时,我尝试加载description.xml但程序在setContentView(descriptionView);
我的查询是
这对我正在做的事情有意义吗?有没有更好的方法呢?
程序崩溃可能是什么问题?
LOGCAT
03-17 06:15:24.918: E/InputEventReceiver(2093): Exception dispatching input event.
03-17 06:15:24.928: E/MessageQueue-JNI(2093): Exception in MessageQueue callback: handleReceiveCallback
03-17 06:15:25.048: E/MessageQueue-JNI(2093): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:292)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.app.Activity.setContentView(Activity.java:1901)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at sg.SanThit.TrackMe.MainActivity$1.onFocusChange(MainActivity.java:117)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.onFocusChanged(View.java:4587)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.widget.TextView.onFocusChanged(TextView.java:7325)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.handleFocusGainInternal(View.java:4375)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.requestFocusNoSearch(View.java:6660)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.requestFocus(View.java:6639)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.requestFocus(View.java:6606)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.requestFocus(View.java:6585)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.onTouchEvent(View.java:8184)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.widget.TextView.onTouchEvent(TextView.java:7377)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.dispatchTouchEvent(View.java:7239)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.app.Dialog.dispatchTouchEvent(Dialog.java:740)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.View.dispatchPointerEvent(View.java:7419)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.os.MessageQueue.nativePollOnce(Native Method)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.os.MessageQueue.next(MessageQueue.java:125)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.os.Looper.loop(Looper.java:124)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 06:15:25.048: E/MessageQueue-JNI(2093): at dalvik.system.NativeStart.main(Native Method)
03-17 06:15:25.048: D/AndroidRuntime(2093): Shutting down VM
03-17 06:15:25.062: W/dalvikvm(2093): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-17 06:15:25.168: E/AndroidRuntime(2093): FATAL EXCEPTION: main
03-17 06:15:25.168: E/AndroidRuntime(2093): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:292)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.app.Activity.setContentView(Activity.java:1901)
03-17 06:15:25.168: E/AndroidRuntime(2093): at sg.SanThit.TrackMe.MainActivity$1.onFocusChange(MainActivity.java:117)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.onFocusChanged(View.java:4587)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.widget.TextView.onFocusChanged(TextView.java:7325)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.handleFocusGainInternal(View.java:4375)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.requestFocusNoSearch(View.java:6660)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.requestFocus(View.java:6639)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.requestFocus(View.java:6606)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.requestFocus(View.java:6585)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.onTouchEvent(View.java:8184)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.widget.TextView.onTouchEvent(TextView.java:7377)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.dispatchTouchEvent(View.java:7239)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.app.Dialog.dispatchTouchEvent(Dialog.java:740)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.View.dispatchPointerEvent(View.java:7419)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.os.MessageQueue.nativePollOnce(Native Method)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.os.MessageQueue.next(MessageQueue.java:125)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.os.Looper.loop(Looper.java:124)
03-17 06:15:25.168: E/AndroidRuntime(2093): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-17 06:15:25.168: E/AndroidRuntime(2093): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 06:15:25.168: E/AndroidRuntime(2093): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 06:15:25.168: E/AndroidRuntime(2093): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 06:15:25.168: E/AndroidRuntime(2093): at dalvik.system.NativeStart.main(Native Method)
03-17 06:20:25.548: I/Process(2093): Sending signal. PID: 2093 SIG: 9