开发者

Start new activity from a listview [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Android ListView, start new activity

Hey can someone tell me how I can start an activity by pressing an item in a listview?

I have fixed it i think because i get no error messages, but when i start this activity (Videos) the app crashes and wants to force close. Whats the problem?

Here is the source code:

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.l开发者_运维知识库iveshit (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


Remove that

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

and replace it with that

ListView listview = getListView();

You're using a ListActivity. You may not set a content view on a ListActivity(it already contains a ListView)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜