从导航抽屉启动另一个列表视图

时间:2015-01-07 17:26:39

标签: java android listview

我无法从当前导航抽屉列表视图移动到另一个listview活动。 从MyActivity到OS。在公共空白选择()下查看案例3。

MainActivity.java

  public void SelectItem(int possition) {

        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (possition) {
            case 0:
                // Home
                fragment = new FragmentOne();
               args.putString(FragmentOne.ITEM_NAME, (String) getText(R.string.desc));
               args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
                break;
            case 1:

                fragment = new FragmentHome();
                break;

            case 2:

                fragment = new FragmentThree();

                break;
            case 3:                

                **Intent menu = new Intent(this,OperatingSystem.class);
                startActivity(menu);**


                break;
            case 4:

                fragment = new fragmentFour();

                break;
            case 5:
                // About
                fragment = new FragmentFive();

                break;


            default:
                break;
        }

        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                .commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);

    }

OperatingSystem.java

    package com.example.faez.bodyalarm;

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

    public class OperatingSystem extends ListActivity {
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };
            // use your custom layout
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                    R.layout.listview, R.id.mainListView, values);
            setListAdapter(adapter);
            setContentView(R.layout.listview);
        }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            String item = (String) getListAdapter().getItem(position);
            Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
        }
    }

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.faez.bodyalarm" >

    <application
        android:debuggable="true"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity android:name=".MainSplashScreen">




            </activity>

        <activity

            android:name=".MyActivity"
            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=".OperatingSystem"></activity>



    </application>

</manifest>

真的很感激,如果你们能提供帮助的话。在构建期间没有问题它只是在手机上调试时停止。

2 个答案:

答案 0 :(得分:0)

case 3中的

您应该在return;之后添加startActivity(menu);因为在切换案例之后您还有一些代码会因为您的&#34;片段而继续崩溃。 var将保持为null。

答案 1 :(得分:0)

尝试检查OperatingSystem.java。我认为setContentView()中不需要ListActivity

同样对于ArrayAdapter,您可能希望放置一个包含TextView及其相应ID的布局。

示例

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

<强> row_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/text1" />