从列表视图开始新活动

时间:2011-09-07 20:28:24

标签: android listview android-activity new-operator

  

可能重复:
  Android ListView, start new activity

嘿,有人可以通过按列表视图中的项目告诉我如何开始活动吗?

我已修复它我认为因为我没有收到任何错误消息,但是当我开始此活动(视频)时,应用程序崩溃并想要强行关闭。问题是什么?

以下是源代码:

package com.alpha.liveshit;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Videos extends ListActivity {

    String[] elements = {"video", "menu"};

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.videos); 
        ListView listview = (ListView)findViewById(R.id.listView1);
        listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, elements));
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {

        if ("video".equals(elements[position])) { 
            Intent myIntent = new Intent(Videos.this, Lars.class);
            startActivity(myIntent);
        }
        else if ("menu".equals(elements[position])) {
            Intent myIntent = new Intent(Videos.this, MenuActivity.class);
            startActivity(myIntent);
        }
    }
}

logcat的

09-07 20:34:21.559: ERROR/AndroidRuntime(326): FATAL EXCEPTION: main
09-07 20:34:21.559: ERROR/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alpha.liveshit/com.alpha.liveshit.Videos}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.os.Looper.loop(Looper.java:123)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at java.lang.reflect.Method.invoke(Method.java:521)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at dalvik.system.NativeStart.main(Native Method)
09-07 20:34:21.559: ERROR/AndroidRuntime(326): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ListActivity.onContentChanged(ListActivity.java:245)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.Activity.setContentView(Activity.java:1647)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at com.alpha.liveshit.Videos.onCreate(Videos.java:17)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 20:34:21.559: ERROR/AndroidRuntime(326):     ... 11 more
09-07 20:34:21.569: WARN/ActivityManager(60):   Force finishing activity com.alpha.liveshit/.Videos
09-07 20:34:21.580: WARN/ActivityManager(60):   Force finishing activity com.alpha.liveshit/.MenuActivity
09-07 20:34:22.079: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{450915a0 com.alpha.liveshit/.Videos}
09-07 20:34:32.477: WARN/ActivityManager(60): Activity destroy timeout for HistoryRecord{45140c78 com.alpha.liveshit/.MenuActivity}
09-07 20:34:32.479: WARN/ActivityManager(60): Activity destroy timeout for HistoryRecord{450915a0 com.alpha.liveshit/.Videos}
09-07 20:34:35.058: INFO/Process(326): Sending signal. PID: 326 SIG: 9
09-07 20:34:35.229: INFO/ActivityManager(60): Process com.alpha.liveshit (pid 326) has died.
09-07 20:34:35.229: INFO/WindowManager(60): WIN DEATH: Window{4508ddb0 com.alpha.liveshit/com.alpha.liveshit.MenuActivity paused=true}
09-07 20:35:45.255: DEBUG/SntpClient(60): request time failed: java.net.SocketException: Address family not supported by protocol
09-07 20:37:00.329: DEBUG/dalvikvm(60): GC_FOR_MALLOC freed 19005 objects / 859600 bytes in 249ms

1 个答案:

答案 0 :(得分:0)

删除

setContentView(R.layout.videos); 
ListView listview = (ListView)findViewById(R.id.listView1);

并将其替换为

ListView listview = getListView();

您正在使用ListActivity。您可能无法在ListActivity上设置内容视图(它已包含ListView)