0

我试图调试我的菜单活动,但它似乎无法显示输出。屏幕上的错误消息显示 Savings Guider(我的应用程序名称)已停止工作。如果我从 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
4

2 回答 2

0

您在代码中进一步使用了三个类,即 SavingsGuiderAppActivity.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>
于 2012-07-22T02:53:21.173 回答
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() 中尝试此操作。

于 2012-07-22T03:31:05.627 回答