我的FragmentPhysics.java代码
public class FragmentPhysics extends Fragment{
private ListView lvPhy, lvPhyDownloaded;
ArrayList<HashMap<String, String>> phyList, phyListDownloaded;
public FragmentPhysics() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Activity activity = getActivity();
Intent intent= activity.getIntent();
phyList = (ArrayList<HashMap<String,String>>) intent.getSerializableExtra("phyList");
phyListDownloaded = (ArrayList<HashMap<String,String>>) intent.getSerializableExtra("phyListDownloaded");
View view = inflater.inflate(R.layout.fragment_physics, container, false);
lvPhy = (ListView) view.findViewById(android.R.id.text1);
lvPhyDownloaded = (ListView) view.findViewById(android.R.id.text2);
ListAdapter adapter = new SimpleAdapter(activity, phyList,
R.layout.list_item, new String[]{ "subject","date", "chapter", "info", "idfile", "downloadlink"},
new int[]{R.id.subject, R.id.date, R.id.chapter, R.id.info, R.id.idfile, R.id.downloadlink});
ListAdapter adapter2 = new SimpleAdapter(activity, phyListDownloaded,
R.layout.list_item_downloaded, new String[]{ "subject","date", "chapter", "info", "idfile", "downloadlink"},
new int[]{R.id.subject, R.id.date, R.id.chapter, R.id.info, R.id.idfile, R.id.downloadlink});
lvPhy.setAdapter(adapter);
lvPhyDownloaded.setAdapter(adapter2);
return view;
}
我的fragment_physics.xml代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/phy_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/text1">
</ListView>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/text2">
</ListView>
</LinearLayout>
</ScrollView>
如果您需要我的MainActivity Code,
getIntent().putExtra("phyList", phyList);
getIntent().putExtra("phyListDownloaded", phyListDownloaded);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentPhysics fragmentPhysics = new FragmentPhysics();
fragmentManager.beginTransaction()
.replace(R.id.phy_layout, fragmentPhysics).commit();
因此代码中没有错误,但是当我运行代码时,活动就会中断。
查看了很多Stackoverflow问题,甚至this,这与我的问题非常相似,但这并不能解决我的问题。
如果我只是注释掉lvPhy.setAdapter(adapter);
lvPhyDownloaded.setAdapter(adapter2);
,一切都运行正常,但是一个空白屏幕(如预期的那样)。有人请帮忙。我是android新手,这是我在Stackoverflow中的第一个问题。
我得到的错误是
FATAL EXCEPTION: main
Process: com.pariksit.insightacademy, PID: 3780
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:100)
at android.widget.ListView.setAdapter(ListView.java:491)
at com.pariksit.insightacademy.FragmentPhysics.onCreateView(FragmentPhysics.java:57)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18811)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
at android.view.Chore
答案 0 :(得分:0)
看起来您传递给SimpleAdapter
的列表为空。当您尝试从extras
获取列表时null
导致此错误。