-1

我想使用一个简单的列表视图,但是当我运行我的程序时,我得到了错误。

public class ListViewActivity extends Activity{
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
         setContentView(R.layout.list);

         ListView listView = (ListView) findViewById(R.id.listView1);
         String[] values = new String[]{"Number 1", "Number 2", "Number 3"};

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, values);

         listView.setAdapter(adapter);

         listView.setOnItemClickListener(new OnItemClickListener() {

             public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
                 // TODO Auto-generated method stub
                 Toast.makeText(ListViewActivity.this, "Click ListItem Number " + position
                    , Toast.LENGTH_LONG).show();
        }
    });
}
}

现在,你能告诉我我的问题在哪里吗?

更新:我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

显现 :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.animeparadise"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".AnimeParadiseT1"
        android:label="@string/title_activity_anime_paradise_t1" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/title_activity_anime_paradise_t1" android:name=".ViewActivity" android:screenOrientation="portrait">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.intent.action.MAIN"/>
        </intent-filter>
    </activity>
    <activity android:label="@string/title_activity_anime_paradise_t1" android:name=".AboutActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".ListActivity" android:label="@string/title_activity_anime_paradise_t1">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

日志猫:

09-04 16:04:23.485: D/dalvikvm(7779): GC_EXTERNAL_ALLOC freed 46K, 51% free 2686K/5379K, external 0K/0K, paused 67ms
09-04 16:04:25.917: D/AndroidRuntime(7779): Shutting down VM
09-04 16:04:25.917: W/dalvikvm(7779): threadid=1: thread exiting with uncaught exception (group=0x40195560)
09-04 16:04:25.927: E/AndroidRuntime(7779): FATAL EXCEPTION: main
09-04 16:04:25.927: E/AndroidRuntime(7779): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.animeparadise/com.animeparadise.ListViewActivity}; have you declared this activity in your AndroidManifest.xml?
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.app.Activity.startActivityForResult(Activity.java:2827)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.app.Activity.startActivity(Activity.java:2933)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at com.animeparadise.AnimeParadiseT1$1.onClick(AnimeParadiseT1.java:30)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.view.View.performClick(View.java:2506)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.view.View$PerformClick.run(View.java:9112)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.os.Handler.handleCallback(Handler.java:587)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.os.Looper.loop(Looper.java:130)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at android.app.ActivityThread.main(ActivityThread.java:3835)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at java.lang.reflect.Method.invoke(Method.java:507)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-04 16:04:25.927: E/AndroidRuntime(7779):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

3

更改"<activity android:name=".ListActivity" to ".ListViewActivity"您的清单文件。

有你的错误......

于 2012-09-04T11:39:56.057 回答
1
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, values);

尝试这个。

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                      android.R.layout.simple_list_item_1, values);
于 2012-09-04T11:29:07.563 回答
0

加上fill_parent的宽度和高度LinearLayout以及 的宽度ListView

看到你的清单后编辑。您有一个名为 ListViewActivity 的 Activity,但您声明:

<activity android:name=".ListActivity" android:label="@string/title_activity_anime_paradise_t1">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

将其更改为ListViewActivity, 和第二个:带有 MAIN 和 LAUNCHER 的意图过滤器应该只用于第一个Activity,它在您的应用程序启动时显示。

于 2012-09-04T11:26:06.197 回答