我以显示两列列表的ACTIVITY为例,并尝试分片实现。活动中没有提出任何问题。 片段应用程序崩溃了
android的新手,在ADK中没有显示错误,以其他方式尝试了不起作用。
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ekatechhp.pkmapplication, PID: 10662
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.ekatechhp.pkmapplication.fragment.FourFragment.onCreateView(FourFragment.java:36)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:631)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:612)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2831)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1608)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1601)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1569)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1874)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:2059)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Fourfragment.java:
@SuppressLint("ValidFragment")
public class FourFragment extends Fragment {
private ListView listView;
private MovieAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_four, container,
false);
listView = (ListView)getActivity().findViewById(R.id.movie_list);
ArrayList<Movie> moviesList = new ArrayList<>();
moviesList.add(new Movie("After Earth" , "2013"));
moviesList.add(new Movie("Baby Driver" , "2017"));
moviesList.add(new Movie("Deadpool" , "2016"));
moviesList.add(new Movie("Divergent" , "2014"));
moviesList.add(new Movie("Fight Club" , "1999"));
moviesList.add(new Movie("Jaws" , "1975"));
moviesList.add(new Movie("Pirates of the Caribbean" , "2011"));
mAdapter = new MovieAdapter(getActivity(),moviesList);
listView.setAdapter(mAdapter);
return view;
}}
MovieAdapter:
public class MovieAdapter extends ArrayAdapter<Movie> {
private Context mContext;
private List<Movie> moviesList = new ArrayList<>();
public MovieAdapter(@NonNull Context context, ArrayList<Movie> list) {
super(context, 0 , list);
mContext = context;
moviesList = list;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull
ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem =
LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);
Movie currentMovie = moviesList.get(position);
TextView name = (TextView) listItem.findViewById(R.id.textView_name);
name.setText(currentMovie.getmName());
TextView release = (TextView)
listItem.findViewById(R.id.textView_release);
release.setText(currentMovie.getmRelease());
return listItem;
}}
fragmentfour.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.ekatechhp.pkmapplication.
fragment.FourFragment">
<ListView
android:id="@+id/movie_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
list_item.xml:
<?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">
<TextView
android:id="@+id/textView_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView_release"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView_name"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:textColor="@color/colorAccent"/>