ListView没有显示

时间:2012-09-04 11:20:09

标签: android listview

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

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>

logCat:

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)

3 个答案:

答案 0 :(得分:3)

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

你的错误.....

答案 1 :(得分: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);

答案 2 :(得分:0)

fill_parent的宽度和高度以及LinearLayout的宽度中添加ListView

看到你Manifest后编辑。 您有一个名为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的intent-filter应仅适用于启动时为您启用的第一个Activity