0

我想我应该展示整个布局结构,以便您找出问题所在。我是 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
4

1 回答 1

0

(1)这对我所做的有意义吗?有没有更好的方法呢?

在我看来,你的部分问题是你正在膨胀并试图使用太多Layouts. 充气没Views问题,但似乎你试图使用太多(到目前为止我可以看到 3 个)。如果你需要的话,如果你打电话给setContentView()你的onCreate()then 可能会更简单和更干净。如果您希望所有这些都合二为一并且使用 API 11 或更高版本,那么您可能需要考虑使用FragmentsinflateViewActivity

很难准确地描绘出你所拥有的东西,但如果每个人里面layout没有很多Views东西,那么将它们放在一个中layout并设置visibilitygone你需要的时候可能会更容易,view然后将其更改为visible

What could be the problem due to which the program crashed?

编辑

错误是因为您有 aListActivity但您ListView的 xml 中没有具有正确 id 的 a 。你需要有类似的东西

<ListView
android:id="@android:id/list"
.../>

如果您只扩展Activity和创建自己的listview,那么您不需要这个。但是如果你扩展ListActivity,那么你需要ListView在你的 xml 中使用它。您的 logcat 中的以下行是您如何知道这一点的

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
于 2013-03-17T06:05:55.347 回答