Android-单击列表视图项时活动组未启动新活动

发布于 2024-11-24 06:57:55 字数 6854 浏览 1 评论 0原文

我目前有一个带有 3 个选项卡的 tabview。 1 个选项卡加载一个 ActivityGroup,然后加载一个 listview。我有这个,所以我可以加载不同的活动并查看并仍然显示选项卡。当我单击 listview 上的第一个项目时,它会完美地加载所有内容并完全按照我想要的方式执行。但是,当我单击列表中的第二项时,我收到日志猫错误。它应该加载新的活动和新的视图。我不确定我是否正确执行此操作,并且非常感谢您的帮助。下面的代码和日志猫。

播放列表组 类:

public class PlaylistGroup extends ActivityGroup{

    public static PlaylistGroup group;

    // Need to keep track of the history if you want the back-button to work properly, 
    //don't use this if your activities requires a lot of memory.

    private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.history = new ArrayList<View>();
      group = this;

          // Start the root activity within the group and get its view
      View view = getLocalActivityManager().startActivity("PlaylistActivity", new
                                        Intent(this,PlaylistActivity.class)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                        .getDecorView();

          // Replace the view of this ActivityGroup
      replaceView(view);

   }

public void replaceView(View v) {
            // Adds the old one to history
    history.add(v);
            // Changes this Groups View to the new View.
    setContentView(v);
}

//so you can go back and forth and keep the tab layout
public void back() {
    if(history.size() > 0) {
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));
    }else {
        finish();
    }
}

public void onBackPressed() {
    PlaylistGroup.group.back();
    return;
}

ListView 类:

public class PlaylistActivity extends ListActivity{

    private static final String TAG = PlaylistActivity.class.getSimpleName();

    // Data to put in the ListAdapter
    private String[] sdrPlaylistNames = new String[] {
            "Best of June 2011", "Best of May 2011", "Dubstep",
            "House", "Other"};

    private ListAdapter sdrListAdapter;
    Intent playbackServiceIntent, playbackServiceIntent1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlists_layout);
        //fill the screen with the list adapter
        playlistFillData();
    }

    public void playlistFillData() {
        //create and set up the Array adapter for the list view
        ArrayAdapter sdrListAdapter = new ArrayAdapter(this, R.layout.list_item, sdrPlaylistNames);
        setListAdapter(sdrListAdapter);
    }

    //set up the on list item Click 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //create a switch so that each list item is a different playlist
        switch(position){
        case 0:
            Intent BOJintent = new Intent(this, BOJAudioActivity.class);

            // Create the view using PlaylistGroup's LocalActivityManager
            View view = PlaylistGroup.group.getLocalActivityManager()
            .startActivity("show_city", BOJintent
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

            // Again, replace the view
            PlaylistGroup.group.replaceView(view);

            playbackServiceIntent = new Intent(this, BOJAudioService.class);
            Log.d(TAG, "Made Intent");
            startService(playbackServiceIntent);
            Log.d(TAG, "started Service");

            break;
        case 1:
                Log.d(TAG, "stop service");
            getApplicationContext().stopService(playbackServiceIntent);
                Log.d(TAG, "service stopp'd");

            Intent BOJintenttest = new Intent(this, TestActivity.class);
            // Create the view using PlaylistGroup's LocalActivityManager
            View view2 = PlaylistGroup.group.getLocalActivityManager()
            .startActivity("show_city", BOJintenttest
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

            // Again, replace the view
           //PlaylistGroup.group.replaceView(view2);
            //  Log.d(TAG, "Made Intent");
            //      startActivity(BOJintenttest);
            //  Log.d(TAG, "started a");

            break;
        case 2:
            break;

        }


    }


}

LOG CAT:

07-14 12:19:12.291: ERROR/AndroidRuntime(6810): FATAL EXCEPTION: main
07-14 12:19:12.291: ERROR/AndroidRuntime(6810): android.content.ActivityNotFoundException: Unable to find explicit activity class {ravebox.dev.sdr/ravebox.dev.sdr.TestActivity}; have you declared this activity in your AndroidManifest.xml?
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:1568)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at ravebox.dev.sdr.PlaylistActivity.onListItemClick(PlaylistActivity.java:74)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.ListView.performItemClick(ListView.java:3513)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Handler.handleCallback(Handler.java:587)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Looper.loop(Looper.java:123)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at java.lang.reflect.Method.invoke(Method.java:507)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at dalvik.system.NativeStart.main(Native Method)

I currently have a tabview with 3 tabs. 1 tab loads an ActivityGroup which then loads a listview. I have this so I can load different activities and view and still display the tabs. When I click the first item on my listview it loads everything perfectly and does exactly what I want it to do. However, when I click on the second item on the list I get a log cat error. It should load a new activity and new view. I'm not sure if I am doing this correctly and would really appreciate the help. Code and Log cat below.

Playlist group Class:

public class PlaylistGroup extends ActivityGroup{

    public static PlaylistGroup group;

    // Need to keep track of the history if you want the back-button to work properly, 
    //don't use this if your activities requires a lot of memory.

    private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.history = new ArrayList<View>();
      group = this;

          // Start the root activity within the group and get its view
      View view = getLocalActivityManager().startActivity("PlaylistActivity", new
                                        Intent(this,PlaylistActivity.class)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                        .getDecorView();

          // Replace the view of this ActivityGroup
      replaceView(view);

   }

public void replaceView(View v) {
            // Adds the old one to history
    history.add(v);
            // Changes this Groups View to the new View.
    setContentView(v);
}

//so you can go back and forth and keep the tab layout
public void back() {
    if(history.size() > 0) {
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));
    }else {
        finish();
    }
}

public void onBackPressed() {
    PlaylistGroup.group.back();
    return;
}

ListView Class:

public class PlaylistActivity extends ListActivity{

    private static final String TAG = PlaylistActivity.class.getSimpleName();

    // Data to put in the ListAdapter
    private String[] sdrPlaylistNames = new String[] {
            "Best of June 2011", "Best of May 2011", "Dubstep",
            "House", "Other"};

    private ListAdapter sdrListAdapter;
    Intent playbackServiceIntent, playbackServiceIntent1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlists_layout);
        //fill the screen with the list adapter
        playlistFillData();
    }

    public void playlistFillData() {
        //create and set up the Array adapter for the list view
        ArrayAdapter sdrListAdapter = new ArrayAdapter(this, R.layout.list_item, sdrPlaylistNames);
        setListAdapter(sdrListAdapter);
    }

    //set up the on list item Click 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //create a switch so that each list item is a different playlist
        switch(position){
        case 0:
            Intent BOJintent = new Intent(this, BOJAudioActivity.class);

            // Create the view using PlaylistGroup's LocalActivityManager
            View view = PlaylistGroup.group.getLocalActivityManager()
            .startActivity("show_city", BOJintent
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

            // Again, replace the view
            PlaylistGroup.group.replaceView(view);

            playbackServiceIntent = new Intent(this, BOJAudioService.class);
            Log.d(TAG, "Made Intent");
            startService(playbackServiceIntent);
            Log.d(TAG, "started Service");

            break;
        case 1:
                Log.d(TAG, "stop service");
            getApplicationContext().stopService(playbackServiceIntent);
                Log.d(TAG, "service stopp'd");

            Intent BOJintenttest = new Intent(this, TestActivity.class);
            // Create the view using PlaylistGroup's LocalActivityManager
            View view2 = PlaylistGroup.group.getLocalActivityManager()
            .startActivity("show_city", BOJintenttest
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

            // Again, replace the view
           //PlaylistGroup.group.replaceView(view2);
            //  Log.d(TAG, "Made Intent");
            //      startActivity(BOJintenttest);
            //  Log.d(TAG, "started a");

            break;
        case 2:
            break;

        }


    }


}

LOG CAT:

07-14 12:19:12.291: ERROR/AndroidRuntime(6810): FATAL EXCEPTION: main
07-14 12:19:12.291: ERROR/AndroidRuntime(6810): android.content.ActivityNotFoundException: Unable to find explicit activity class {ravebox.dev.sdr/ravebox.dev.sdr.TestActivity}; have you declared this activity in your AndroidManifest.xml?
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:1568)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at ravebox.dev.sdr.PlaylistActivity.onListItemClick(PlaylistActivity.java:74)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.ListView.performItemClick(ListView.java:3513)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Handler.handleCallback(Handler.java:587)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.os.Looper.loop(Looper.java:123)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at java.lang.reflect.Method.invoke(Method.java:507)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-14 12:19:12.291: ERROR/AndroidRuntime(6810):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2024-12-01 06:57:55

设置 AndroidManifest.xml 的活动部分,如下所示:

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".FirstActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".TestActivity"></activity>

</application>

Set up your activities section of the AndroidManifest.xml like this:

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".FirstActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".TestActivity"></activity>

</application>
爱已欠费 2024-12-01 06:57:55

您确定已将 TestActivity 声明为 AndroidManifest.xml 中的活动吗?

Are you sure you declared TestActivity to be an activity in your AndroidManifest.xml?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文