2017-01-18 151 views
0

我下面的例子在一本書中的步驟,並與仿真器進行調試時,我得到幾個錯誤:InflateException與Android <fragment>

E/AndroidRuntime:致命異常:主要 過程:activity.tasks。 dummies.com.tasklistactivity,PID:2995 java.lang.RuntimeException:無法啓動活動ComponentInfo {activity.tasks.dummies.com.tasklistactivity/activity.tasks.dummies.com.tasklistactivity.TaskListActivity}:android.view.InflateException :二進制XML文件行#15:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)錯誤的類擴展片段 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access $ 800(ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278 )在android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 在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:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 原因:android.view.InflateException:二進制XML文件行#15:錯誤擴展類片段 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) at android.app.Activity.setContentView(Activity.java:2144) at activity.tasks.dummies.com.tasklistactivity .TaskListActivity.onCreate(TaskListActivity.java:25) 在android.app.Activity.performCreate(Activity.java:5937) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access $ 800(ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java .lang.reflect.Metho d.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) at com .android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 引起:android.app.Fragment $ InstantiationException:試圖實例化一個類activity.tasks.dummies.com.tasklistactivity.TaskListFragment不是片段 在android.app.Fragment.instantiate(Fragment.java:606) 在android.app.Fragment.instantiate(Fragment.java:582) 在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) 在android.app.Activity.onCreateView(Activity.java:5286) 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377) at android。 app.Activity.setContentView(Activity.java:2144) at activity.tasks.dummies.com.tasklistactivity.TaskListActivity.onCreate(TaskListActivity.java:25) at android.app.Activity.performCreate(Activity.java:5937) at android.app.Instrumenta tion.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread。訪問$ 800(ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper .loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method。調用(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 引起:java.lang。 ClassCastException異常 在android.app.Fragment.instantiate(Fragment.java:606) 在android.app.Fragment.instantiate(Fragment.java:582) 在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) 在android.app.Activity.onCreateView(Activity.java:5286) 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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow。 java:377) at android.app.Activity.setContentView(Activity.java:2144) at activity.tasks.dummies.com.tasklistactivity.TaskListActivity.onCreate(TaskListActivity.java:25) at android.app.Activity。在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) android.app.ActivityThread.handleLaunchActivity()方法執行創建(Activity.java:5937) (android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 活性狀況在android.app.ActivityThread.access $ 800(ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android。java.lang.reflect.Method.invoke(Method.java:372) internal.os.ZygoteInit.main(ZygoteInit。Java的:694)

的相關的Java文件(TaskListFragment.java):

package activity.tasks.dummies.com.tasklistactivity; 
 

 

 
import android.os.Bundle; 
 
import android.support.v4.app.Fragment; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 

 

 
import android.support.v7.widget.LinearLayoutManager; 
 
import android.support.v7.widget.RecyclerView; 
 

 

 
import activity.tasks.dummies.com.adapter.TaskListAdapter; 
 

 

 
public class TaskListFragment extends Fragment 
 
    { 
 

 
     RecyclerView recyclerView; 
 
     TaskListAdapter adapter; 
 

 
     public TaskListFragment() 
 
     { 
 
      // Required empty public constructor 
 
     } 
 

 
     @Override 
 
     public void onCreate(Bundle savedInstanceState) 
 
     { 
 
      super.onCreate(savedInstanceState); 
 
      adapter = new TaskListAdapter(); 
 
     } 
 

 
     @Override 
 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
 
     { 
 
      // Inflate the layout for this fragment 
 
      //return inflater.inflate(R.layout.fragment_task_list, container, false); 
 
      final View v = inflater.inflate(R.layout.fragment_task_list, container, false); 
 
      recyclerView = (RecyclerView) v.findViewById(R.id.recycler); 
 

 
      recyclerView.setAdapter(adapter); 
 

 
      recyclerView.setHasFixedSize(true); 
 
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
 
      return v; 
 
     } 
 

 
    }

我的XML佈局(activity_task_list.xml):

<?xml version="1.0" encoding="utf-8"?> 
 

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:orientation="vertical" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
    <Toolbar 
 
     style="?android:actionBarStyle" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:title="@string/app_name" 
 
     android:id="@+id/toolbar"/> 
 

 
    <fragment 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:id="@+id/fragment" 
 
     android:name="activity.tasks.dummies.com.tasklistactivity.TaskListFragment" 
 
     tools:layout="@layout/fragment_task_list"/> 
 
</LinearLayout>

0這是在錯誤中提到

線15是在XML中<片段

我在Android初學者,我不知道如何解決它。

有人可以告訴我什麼是錯的?

感謝

也有另一類(TaskListActivity.java):

package activity.tasks.dummies.com.tasklistactivity; 
 

 

 
import android.app.Activity; 
 
import android.os.Bundle; 
 
import android.widget.Toolbar; 
 

 
    public class TaskListActivity extends Activity 
 
    { 
 

 
     @Override 
 
     protected void onCreate(Bundle savedInstanceState) 
 
     { 
 
      super.onCreate(savedInstanceState); 
 
      setContentView(R.layout.activity_task_list); 
 
      setActionBar((Toolbar) findViewById(R.id.toolbar)); 
 
     } 
 
    }

+0

什麼是你的包叫什麼名字? – rafsanahmad007

+0

我認爲你粘貼了你的XML兩次,而不是你的Java和XML。 – Pietu1998

+0

java文件是TaskListFragment.java,xml是fragment_task_list.xml – user3249244

回答

0

試試這個;

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/fragment" 
    class ="activity.tasks.dummies.com.tasklistactivity.TaskListFragment" 
    tools:layout="@layout/fragment_task_list"/> 
在你活動類

public class TaskListActivity extends FragmentActivity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_task_list); 
     setActionBar((Toolbar) findViewById(R.id.toolbar)); 
    } 
} 

import android.support.v4.app.FragmentActivity;

+0

Nop 。在您的活動flle同樣的問題 – user3249244

+0

..whereü致電'fragment'應該延伸'FragmentActivity'不'Activity' – rafsanahmad007

+0

還有一種類: 公共類TaskListActivity擴展活動 { @覆蓋 保護無效的onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_task_list); setActionBar((Toolbar)findViewById(R.id.toolbar)); } } – user3249244

相關問題