2014-05-14 73 views
0

我正在嘗試學習片段通信Android啓動片段時獲取異常

爲此,我按照教程編寫了示例應用程序。

現在,當啓動我的示例片段時,我收到異常,如下所示。

我附加我的總java代碼也在例外之下。

請幫我解決問題。

05-14 16:10:04.023: E/AndroidRuntime(1361): FATAL EXCEPTION: main 
05-14 16:10:04.023: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment_flexibleui/com.example.fragment_flexibleui.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.os.Looper.loop(Looper.java:137) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at dalvik.system.NativeStart.main(Native Method) 
05-14 16:10:04.023: E/AndroidRuntime(1361): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.Activity.setContentView(Activity.java:1867) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at com.example.fragment_flexibleui.MainActivity.onCreate(MainActivity.java:13) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.Activity.performCreate(Activity.java:5008) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  ... 11 more 
05-14 16:10:04.023: E/AndroidRuntime(1361): Caused by: java.lang.IllegalStateException: Fragment com.example.fragment_flexibleui.FragmentA did not create a view. 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.app.Activity.onCreateView(Activity.java:4687) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 
05-14 16:10:04.023: E/AndroidRuntime(1361):  ... 21 more 

MainActivity.java

public class MainActivity extends Activity implements Communicator{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public void respond(int i) { 
     // TODO Auto-generated method stub 

     FragmentManager manager = getFragmentManager(); 
     FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2); 
     f2.changeData(i);  

    } 


} 

FragmentA.java

public class FragmentA extends Fragment implements OnItemClickListener{ 

ListView lv; 
Communicator communicator; 

public View OnCreateView(LayoutInflater inflater,ViewGroup container,Bundle bundle) 
{ 
    View view = inflater.inflate(R.layout.fragment_a, container, false); 
    return view; 

} 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    communicator = (Communicator)getActivity(); 
    super.onActivityCreated(savedInstanceState); 
    lv = (ListView)getActivity().findViewById(R.id.listView1); 
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1); 
    lv.setAdapter(adapter); 
    lv.setOnItemClickListener(this); 
} 
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) { 

    // TODO Auto-generated method stub 
    communicator.respond(index); 
} 

} 

FragmentB.java

public class FragmentB extends Fragment{ 

TextView textview; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    View view = inflater.inflate(R.layout.fragment_b, container, false); 

    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
    textview = (TextView)getActivity().findViewById(R.id.textView1);   
} 
public void changeData(int i) 
{ 
    Resources res = getResources(); 
    String[] descrStrings = res.getStringArray(R.array.descriptions); 
    textview.setText(descrStrings[i]); 
} 
} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout2" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:background="#F00"> 

<fragment 
    android:id="@+id/fragment1" 
    android:name="com.example.fragment_flexibleui.FragmentA" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<fragment 
    android:id="@+id/fragment2" 
    android:name="com.example.fragment_flexibleui.FragmentB" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

回答

0

更改此

View view = inflater.inflate(R.layout.fragment_a, container, false); 

View view = inflater.inflate(R.layout.fragment_a, null); 

檢查這個轉換工作,因爲我有我的有關疑慮。

communicator = (Communicator)getActivity(); 

新編輯:

public class FragmentB extends Fragment{ 
TextView textview; 
View view; // change this 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    view = inflater.inflate(R.layout.fragment_b, container, false);//change this 

    return view; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 
    textview = (TextView)view.findViewById(R.id.textView1);//Change this 
} 
public void changeData(int i) 
{ 
    Resources res = getResources(); 
    String[] descrStrings = res.getStringArray(R.array.descriptions); 
    textview.setText(descrStrings[i]); 
} 

}

新編輯:

public class MainActivity extends FragmentActivity {//version support 4 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     replaceFragment(new FragmentA()); 
    } 

    private void replaceFragment(Fragment fragment) { 
     FragmentTransaction frgTransaction = getSupportFragmentManager().beginTransaction(); 
     frgTransaction.replace(R.id.frame, fragment); 
     frgTransaction.commit(); 
    } 
} 


//Your main XML 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout2" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" 
android:background="#F00"> 

<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</LinearLayout> 
+0

試着與你的第一個輸入,但沒有用。它仍在崩潰......'05-14 17:16:04.244:E/AndroidRuntime(1031):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.fragment_flexibleui/com.example.fragment_flexibleui.MainActivity} :android.view.InflateException:二進制XML文件行#14:錯誤膨脹類的片段' – user3637502

+0

再次檢查我的答案,我編輯我的迴應。 – Chefes

+0

再次同樣例外 – user3637502