2012-07-22 134 views
0

我試圖調試我的菜單活動,但它似乎不能顯示輸出。屏幕上的錯誤信息表示儲蓄指南(我的應用程序名稱)已停止工作。如果我從android菜單手動點擊我的應用程序,它說我的應用程序沒有安裝。但是,我的代碼中沒有錯誤。任何人都可以幫助我?android應用程序不會啓動

我的菜單活動代碼:

public class SavingsGuiderMenuActivity extends SavingsActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu); 
    ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 
    String[] items = { getResources().getString(R.string.start), 
      getResources().getString(R.string.about), 
      getResources().getString(R.string.help) }; 
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items); 
    menuList.setAdapter(adapt); 
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 
      // Note: if the list was built "by hand" the id could be used. 
      // As-is, though, each item has the same id 
      TextView textView = (TextView) itemClicked; 
      String strText = textView.getText().toString(); 
      if (strText.equalsIgnoreCase(getResources().getString(R.string.start))) { 
       // Launch the app Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAppActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) { 
       // Launch the Help Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderHelpActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.about))) { 
       // Launch the about Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAboutActivity.class)); 
      } 
     } 
    }); 
} 

}

我的清單:

<application 
    android:label="@string/splash" 
    android:debuggable="true" 
    android:icon="@drawable/saving"> 
    <activity 
     android:name=".SavingsGuiderMenuActivity" 
     android:label="@string/splash"> 
     <intent-filter> 
      <action 
       android:name="android.intent.action.MAIN" /> 
      <category 
       android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

logcat的錯誤

07-22 03:18:24.712: E/ArrayAdapter(806): You must supply a resource ID for a TextView 
07-22 03:18:24.772: E/AndroidRuntime(806): FATAL EXCEPTION: main 
07-22 03:18:24.772: E/AndroidRuntime(806): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.AbsListView.obtainView(AbsListView.java:2033) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.ListView.onMeasure(ListView.java:1155) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:579) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:392) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4671) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4671) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4671) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
07-22 03:18:24.772: E/AndroidRuntime(806): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2062) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.View.measure(View.java:12581) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1038) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.os.Looper.loop(Looper.java:137) 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.app.ActivityThread.main(ActivityThread.java:4340) 
07-22 03:18:24.772: E/AndroidRuntime(806): at java.lang.reflect.Method.invokeNative(Native Method) 
07-22 03:18:24.772: E/AndroidRuntime(806): at java.lang.reflect.Method.invoke(Method.java:511) 
07-22 03:18:24.772: E/AndroidRuntime(806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-22 03:18:24.772: E/AndroidRuntime(806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-22 03:18:24.772: E/AndroidRuntime(806): at dalvik.system.NativeStart.main(Native Method) 
07-22 03:18:24.772: E/AndroidRuntime(806): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView 
07-22 03:18:24.772: E/AndroidRuntime(806): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379) 
07-22 03:18:24.772: E/AndroidRuntime(806): ... 33 more 
+2

查看logcat並從強制關閉應用程序的異常中發佈堆棧跟蹤。 – FoamyGuy 2012-07-22 02:41:38

+0

我的logcat有很多錯誤,而且我看到他們中的大多數與數組適配器有關,還有線性佈局,相對等等,就好像我的xml佈局是我的問題的原因。那可能嗎? – honeybee 2012-07-22 03:10:04

+0

可能是,編輯你的問題,併發布logcat我們將能夠幫助你更多。 – FoamyGuy 2012-07-22 03:11:02

回答

0

不是你的xml是好的我認爲。如果你想創建一個菜單來選擇不同的活動,這可能不是最好的方法。

String classes[] = {"class_a","class_b","class_c"}; 
String class = classes[position]; Class ourClass=Class.forName("package_name"+class); 
Intent intent = new Intent(Menu.this,ourClass); 
startActivity(intent); 

試試這在您的onItemClicked()在嘗試括號。

+0

String class = classes [position]; Class ourClass = Class.forName(「package_name」+ class); <<我在'String class'變量中標記的錯誤包含forName括號內的變量... – honeybee 2012-07-22 03:38:39

+0

String class = classes [position]; Class ourClass = Class.forName(「example.package.com」+ class); 它工作正常。你是否把它放在帶有catch子句的try brackets中? – 2012-07-22 03:44:13

+0

它說,當我將鼠標懸停時,字符串不能解析爲變量。我認爲eclipse讀取我的字符串數據類型作爲變量..? – honeybee 2012-07-22 03:53:56

0

您還使用了三個你的類的代碼即薩文gsGuiderAppActivity.java,SavingsGuiderHelpActivity.java和SavingsGuiderAboutActivity.java。您還需要在清單中註冊它們。 你的清單應該看起來像這樣:

<application 
android:label="@string/splash" 
android:debuggable="true" 
android:icon="@drawable/saving"> 
<activity 
    android:name=".SavingsGuiderMenuActivity" 
    android:label="@string/splash"> 
    <intent-filter> 
     <action 
      android:name="android.intent.action.MAIN" /> 
     <category 
      android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
<activity 
    android:name=".SavingsGuiderAppActivity" 
    android:label="@string/splash"> 
<intent-filter> 
     <action 
      android:name="ACTIVITY_A" /> 
     <category 
      android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 
<activity 
    android:name=".SavingsGuiderHelpActivity" 
    android:label="@string/splash"> 
<intent-filter> 
     <action 
      android:name="ACTIVITY_B" /> 
     <category 
      android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 
<activity 
    android:name=".SavingsGuiderAboutActivity" 
    android:label="@string/splash"> 
<intent-filter> 
     <action 
      android:name="ACTIVITY_C" /> 
     <category 
      android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 
+0

謝謝@Adnan Zahid。我註冊了,但我的應用程序仍然是'未安裝'。我不知道問題出在哪裏。 – honeybee 2012-07-22 02:59:33

+0

嘗試上面的編輯。嘗試了 – 2012-07-22 03:02:34

+0

。是因爲我的XML佈局有問題或什麼,這就是爲什麼它說我的應用程序沒有安裝? – honeybee 2012-07-22 03:08:10

相關問題