2011-02-05 102 views
0

我的初始屏幕基本上來自顯示記錄列表的listactivity。我創建了一個應該打開一個新窗口的菜單,但當我點擊它時總是強制關閉。下面是我的代碼:如何使用菜單從列表活動切換到活動

Guestbook.java (ListActivity) 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
     case R.id.mnuNew: 
      //setContentView(R.layout.form); 
      Intent mIntent = new Intent(getApplicationContext(), Maintenance.class); 
      startActivityForResult(mIntent, 0); 

      //Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

這裏是Maintenance.class

package com.android.guestbook; 

import android.app.Activity; 
import android.os.Bundle; 

public class Maintenance extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.form); 
    } 
} 

代碼有什麼錯我的代碼?

這裏是logcat信息。

02-06 07:02:29.617: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.android.guestbook/.Maintenance } 
02-06 07:02:29.956: DEBUG/AndroidRuntime(276): Shutting down VM 
02-06 07:02:29.956: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main 
02-06 07:02:30.107: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.guestbook/com.android.guestbook.Maintenance}: java.lang.RuntimeException: Binary XML file line #82: You must supply a layout_width attribute. 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276): Caused by: java.lang.RuntimeException: Binary XML file line #82: You must supply a layout_width attribute. 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3592) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3672) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1395) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1321) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:620) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.Activity.setContentView(Activity.java:1647) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at com.android.guestbook.Maintenance.onCreate(Maintenance.java:11) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-06 07:02:30.107: ERROR/AndroidRuntime(276):  ... 11 more 
02-06 07:02:30.216: WARN/ActivityManager(59): Force finishing activity com.android.guestbook/.Maintenance 
02-06 07:02:30.267: WARN/ActivityManager(59): Force finishing activity com.android.guestbook/.GuestBook 

這裏是我的維護類的form.xml。

> <?xml version="1.0" encoding="utf-8"?> 
> <LinearLayout 
> xmlns:android="http://schemas.android.com/apk/res/android" 
> android:layout_width="fill_parent" 
> android:layout_height="fill_parent" 
> android:orientation="vertical"> 
>  <LinearLayout   
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <TextView 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:text="@string/name" /> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <EditText 
>   android:inputType="textPersonName" 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:id="@+id/name" 
>   android:hint="Enter your name here" /> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <TextView 
>   android:layout_width="wrap_content" 
>   android:layout_height="wrap_content" 
>   android:text="@string/email" /> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <EditText 
>   android:inputType="textEmailAddress" 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:id="@+id/email" 
>   android:hint="Enter your email here" /> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <TextView 
>   android:layout_width="wrap_content" 
>   android:layout_height="wrap_content" 
>   android:text="@string/message" /> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <EditText 
>   android:inputType="textMultiLine" 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:id="@+id/message" 
>   android:hint="Enter your message here"></EditText> 
>  </LinearLayout> 
>  <LinearLayout 
>  android:layout_height="wrap_content" 
>  android:layout_width="fill_parent"> 
>   <Button 
>   android:onClick="onClick" 
>   android:layout_weight="1" 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:id="@+id/submit" 
>   android:text="@string/submit" /> 
>   <Button 
>   android:onClick="onClick" 
>   android:layout_weight="1" 
>   android:layout_width="fill_parent" 
>   android:layout_height="wrap_content" 
>   android:id="@+id/cancel" 
>   android:text="@string/cancel" /> 
>  </LinearLayout> 
>  <ListView 
>  android:id="@+id/list" /> </LinearLayout> 
+0

請發佈logcat輸出的錯誤 –

+0

嗨,我如何得到logcat輸出? – adshocker

+0

在Eclipse中使用'adb logcat',DDMS或DDMS透視圖來檢查LogCat並查看與「強制關閉」相關的堆棧跟蹤。 – CommonsWare

回答

0

您是否在您的Manifest文件中聲明瞭Maintenance.class - Activity ?!

你需要把<activity> -Tag在AndroidManifest.xml<application> -Tag之間:

<application android:label="@string/app_name" android:icon="@drawable/icon" android:debuggable="true"> 
    <activity android:name=".activities.Guestbook" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".activities.Maintenance " 
     android:label="@string/app_name"> 
    </activity> 

</application> 
+0

嗨,我已經在Manifest中添加了維護類,但仍然收到相同的強制關閉錯誤。 – adshocker

0

感謝你們的幫助,我終於想通了,什麼是錯的。檢查logcat它說一些關於我的XML佈局layout_width的問題。所以然後我檢查了XML並注意到我有一個沒有任何layout_width屬性的listview。一旦我脫下它開始工作。

再次感謝。

0

一個錯誤是聲明後面兩行參數的ListView小部件在82行的form.xml。

android:layout_height= 
android:layout_width=