我认为我应该呈现整个布局结构,以便您可以找出问题所在。我是Android编程的新手,我不确定我在做什么是正确的。我的main.xml加载了listview。然后我有一个菜单,当按下菜单时,我加载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
答案 0 :(得分:0)
(1)这对我正在做的事情有意义吗?有没有更好的方法呢?
在我看来,你的部分问题是你正在膨胀并试图使用太多Layouts
。膨胀Views
是可以的,但似乎你试图使用太多(目前为止我可以看到3个)。如果您在setContentView()
中调用onCreate()
,那么如果您需要,可以inflate
View
为您的列表,这将更加简单和清洁。如果您希望所有这些都在一个Activity
并使用API 11或更高版本,那么您可能需要考虑使用Fragments
很难准确描述您拥有的内容,但如果每个layout
内部都没有Views
,那么将它们放在一个layout
中可能更容易将visibility
设置为gone
,直到您需要view
,然后将其更改为visible
What could be the problem due to which the program crashed?
修改强>
错误是因为您有一个ListActivity
,但您的xml中没有ListView
且ID正确。你需要像
<ListView
android:id="@android:id/list"
.../>
如果您只展开Activity
并创建自己的listview
,则不需要此项。但是如果你扩展ListActivity
,那么你需要在你的xml中使用ListView
。 logcat中的以下行是您如何知道这个
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'