我認爲我應該展示整個佈局結構,以便您可以弄清楚問題出在哪裏。我是Android編程的新手,我不確定我在做什麼是對的。我有main.xml加載listview。然後我有一個菜單,當按下菜單時,我加載了add.xml以將新列表添加到列表視圖。它如下,工作正常。加載佈局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
張貼錯誤日誌 – MuraliGanesan 2013-03-17 05:56:42
錯誤日誌中的很多錯誤。如何複製到這裏? – Bryanyan 2013-03-17 06:24:09
logcat選項卡中有一個保存按鈕。選擇所有行並保存到日誌文件中,然後將其複製並粘貼到OP中的代碼中。您也可以按類型和其他方式過濾logcat。如果你看看它,並找到引用你的項目的第一行,它應該告訴你行號和上面那個,錯誤的來源,如'空指針異常引起的或類似的東西 – codeMagic 2013-03-17 06:28:20