-3

我是 android 新手,我想创建一个项目,但是在选项卡式活动的片段内插入列表视图时遇到问题,单击时可以传递给另一个活动。在我在这里问之前,我已经尝试过搜索解决方案,但是没有用,希望这里的任何人都可以解决我的问题。

这是我的代码

作为选项卡主机的服务活动

public class ServiceActivity extends AppCompatActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_service);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

//deleted PlaceHolderFragment

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position) {
            case 0:
                Sinspection sins = new Sinspection();
                return sins;
            case 1:
                Minspection mins = new Minspection();
                return mins;
            case 2:
                Linspection lins = new Linspection();
                return lins;
            case 3:
                WRinspection wrins = new WRinspection();
                return wrins;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "S Inspection";
            case 1:
                return "M Inspection";
            case 2:
                return "L Inspection";
            case 3:
                return "When Required";
        }
        return null;
    }
}

片段类

public class Linspection extends ListFragment {

String [] linsp = {
        "Differential Oil and Filter",
        "Hub and Reduction Gear Oil",
        "Wheel Nut",
        "Air Dryer Filter"
};
ArrayAdapter<String> adapter;

ListView listView;

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    listView = (ListView)getActivity().findViewById(R.id.listViewL);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.
            simple_list_item_1,linsp);
    setListAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            switch (position){
                case 0:
                    Intent intent = new Intent(getActivity(), EngineOil.class);
                    startActivity(intent);
            }
        }
    });
    return super.onCreateView(inflater, container, savedInstanceState);
}

我想通过这个活动

public class EngineOil extends AppCompatActivity {

ViewPager viewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.s_engine_oil);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager) findViewById(R.id.viewPager);

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
    viewPager.setAdapter(viewPagerAdapter);
}
}

这是我的xml:

活动服务

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listViewL"
    >
</ListView>
</RelativeLayout>

活动目的地 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="?attr/actionBarTheme"
    android:minHeight="?attr/actionBarSize"
    android:id="@+id/toolbar3"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/viewPager"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/toolbar3"
    android:layout_alignParentStart="true">

</android.support.v4.view.ViewPager>
</RelativeLayout>

当我运行应用程序时,它“总是不断崩溃”。我不知道如何解决它,希望你能帮助我解决我的问题

提前致谢

4

1 回答 1

0

我做了一个项目供你参考。

`activity_main`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/ll_cashier_invoice"
                    android:baselineAligned="false"
                    android:gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="4dip">

                    <fragment
                        android:id="@+id/frag_invoice"
                        android:name="com.example.sydney.sample.MainActivity$FirstFragment"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</RelativeLayout>

`fragment1.xml`

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/myArray" />

 </LinearLayout>

`MainActivity.java`

public class MainActivity extends Activity {

ListView lv;
TabHost host;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    host = (TabHost)findViewById(R.id.tabHost);
    host.setup();
    lv = (ListView)findViewById(R.id.list);


    TabHost.TabSpec spec = host.newTabSpec("Sinspection");

    spec.setContent(R.id.tab1);

    spec.setIndicator("Sinspection");

    host.addTab(spec);

    //Tab 2

    spec = host.newTabSpec("Minspection");

    spec.setContent(R.id.tab2);

    spec.setIndicator("Minspection");

    host.addTab(spec);

    //Tab 3

    spec = host.newTabSpec("Linspection");

    spec.setContent(R.id.tab3);

    spec.setIndicator("Linspection");

    host.addTab(spec);

    //Tab 4

    spec = host.newTabSpec("Winspection");

    spec.setContent(R.id.tab4);

    spec.setIndicator("Winspection");

    host.addTab(spec);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent myIntent = new Intent(MainActivity.this,NextActivity.class);
            startActivity(myIntent);
        }
    });
}

public static class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}
}

点击这里查看完整代码。

于 2017-05-24T03:09:31.567 回答