我正在尝试在我的应用中实施RecyclerView
,但我无法使其正常工作。它引发了NPE,我无法找到原因,因为我已根据Google官方教程完成了所有工作。
Logcat输出:
04-26 23:04:41.823 22356-22356/com.example.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41670d88)
04-26 23:04:41.823 22356-22356/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.app, PID: 22356
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.PastEventActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.example.app.PastEventActivity.onCreate(PastEventActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.app.fragments.PastEventFragment.onCreateView(PastEventFragment.java:34)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1146)
at android.app.Activity.onCreateView(Activity.java:4786)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.example.app.PastEventActivity.onCreate(PastEventActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
PastEventFragment.java
public class PastEventFragment extends Fragment {
private RecyclerView mRecyclerView;
public PastEventFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View pastEvent = inflater.inflate(R.layout.fragment_past_event, container, false);
mRecyclerView = (RecyclerView) pastEvent.findViewById(R.id.pe_rv);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return pastEvent;
}
}
fragment_past_event.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"
android:background="@drawable/backrepeat">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="@+id/pe_rv"
/>
</LinearLayout>
PastEventActivity.java
public class PastEventActivity extends ActionBarActivity {
ActionBar actionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_past_event);
}
}
activity_past_event.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">
<fragment
android:name="com.example.app.fragments.PastEventFragment"
android:id="@+id/fragment_pe"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
这段代码有什么问题?我从其他问题尝试了许多解决方案,但没有一个能为我工作。我正在寻求帮助,因为我将我的代码与官方指南和一些非正式的教程进行了比较,它应该可以正常工作。
答案 0 :(得分:1)
您在布局中使用android:name="@+id/pe_rv"
。
您应该使用android:id="@+id/pe_rv"
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pe_rv"
/>
答案 1 :(得分:0)
请定义XML命名空间,将xmlns:tools="http://schemas.android.com/tools"
放在主命名空间下面的fragment_past_event.xml
LinearLayout
答案 2 :(得分:0)
在观察你的logcat后我认为你有第一个例外,那就是膨胀 RecyclerView 然后 NullPointer 例外。
您是否检查过libs文件夹中的android-support-v7-recyclerview.jar
库?如果是,那么检查您的清单文件是否具有目标sdk版本为21
<uses-sdk
android:minSdkVersion="11" //or anything you need
android:targetSdkVersion="21" />
因为在互联网上尝试了一切之后,我通过将目标从17改为21来解决了recyclelerview膨胀错误。
可能这对未来的某些人有帮助,而不是像我一样浪费时间。
答案 3 :(得分:-1)
你缺少PastEventFragment.java中的setContentView指令我遇到了同样的问题。