编辑:这不是重复,我修复了NullPointerException并且它不再重复。请将其标记为重复。
感谢@Fondesa,我已经修复了NullPointerException。我已阅读其他问题@Tunaki将其链接到了,这很有帮助。但它并没有完全解决我的问题。
我试图在我的主要活动中使用listfragment,但是有android.view.InflateException。任何帮助将非常感激。我现在已经被困在这一天了。
这是错误日志。
FATAL EXCEPTION: main
Process: com.idiots.redundant.digiteyes_beacon, PID: 3626
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idiots.redundant.digiteyes_beacon/com.idiots.redundant.digiteyes_beacon.Exxplore}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2362)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.idiots.redundant.digiteyes_beacon.Exxplore.onCreate(Exxplore.java:148)
at android.app.Activity.performCreate(Activity.java:6072)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1046)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.idiots.redundant.digiteyes_beacon.Exxplore.onCreate(Exxplore.java:148)
at android.app.Activity.performCreate(Activity.java:6072)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Exxplore.java:148 setContentView(R.layout.product_list);
主要活动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">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/authenticationfragment"
android:name="com.idiots.redundant.digiteyes_beacon.authentication"
/>
<fragment
android:layout_below="@+id/toolbar"
android:id="@+id/productlistfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.idiots.redundant.digiteyes_beacon.ADlister"
/>
<fragment
android:id="@+id/ctafragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.idiots.redundant.digiteyes_beacon.fragCTA"
/>
<android.support.v4.widget.DrawerLayout 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:layout_below="@+id/toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/cardview_dark_background"
tools:context="com.idiots.redundant.digiteyes_beacon.Exxplore">
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/prodlistview"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
失败的片段类。
public class ADlister extends ListFragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
private fragCTA.OnFragmentInteractionListener mListener;
ListView listView;
ArrayList <Result> results;
FragmentListAdapter adapter;
public ADlister() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_adlister, container, false);
listView =(ListView)view.findViewById(R.id.prodlistview_fragement);
Exxplore exxplore= (Exxplore) getActivity();
results=exxplore.getlist();
if(results!=null&&results.size()!=0)
lister();
return view;
}
void lister(){
int count=results.size();
String produrls[]=new String[count];
String prodname[]=new String[count];
for(int i=0;i<count;i++){
prodname[i]=results.get(i).getProduct_title();
produrls[i]=results.get(i).getProduct_url();
}
this.setListAdapter(adapter);
assert listView != null;
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onRefresh() {
Exxplore exxplore= (Exxplore) getActivity();
results=exxplore.getlist();
lister();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
函数exxplore.getlist()
返回Result类型的数组列表。
答案 0 :(得分:0)
首先,您需要在com.idiots.redundant.digiteyes_beacon.ADlister.lister(ADlister.java:64)
解决此错误Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.idiots.redundant.digiteyes_beacon.ADlister.lister(ADlister.java:64)
at com.idiots.redundant.digiteyes_beacon.ADlister.onCreateView(ADlister.java:50)
答案 1 :(得分:0)
修改强>
编辑完成后,您必须检查onCreateView()
中的空列表:
if(results != null && results.size()!=0)
lister();
答案 2 :(得分:0)
您应该为结果添加空检查 -
results = exxplore.getlist();
if (results != null && results.size() != 0)
lister();
return view;
答案 3 :(得分:0)
Exxplore exxplore= (Exxplore) getActivity();
在Activity
方法中获取onCreateView()
实例并不理想。请使用onAttach(Activity mActivity)
方法获取您的活动实例。