我想在平板視圖中顯示列表+說明。以下代碼在縱向模式下正常工作,但在橫向模式下時停止。有什麼問題嗎?二進制XML文件行#20:錯誤擴展類片段Android
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class MainActivity extends Activity {
// if run on phone, isSinglePane = true
// if run on tablet, isSinglePane = false
boolean isSinglePane;
static String[] month ={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
public static class MyListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListAdapter myArrayAdapter =
new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, month);
setListAdapter(myArrayAdapter);
}
}
public static class MyDetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.layout_detailfragment, null);
return view;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.phone_container);
if(v == null){
//it's run on tablet
isSinglePane = false;
/*
* MyListFragment and MyDetailFragment have been loaded in XML,
* no need load.
*/
}else{
//it's run on phone
//Load MyListFragment programmatically
isSinglePane = true;
if(savedInstanceState == null){
//if's the first time created
MyListFragment myListFragment = new MyListFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.phone_container, myListFragment);
fragmentTransaction.commit();
}
}
}
}
佈局/ activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
RES /佈局-脊/ activitymain.xml
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal" />
<FrameLayout
android:id="@+id/phone_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
LayoutdetailFragment.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_detailfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detail Fragment"/>
</LinearLayout>
錯誤
E/AndroidRuntime:致命異常:主 工藝:com.example.fyp_awais.tab,PID:3974 了java.lang.RuntimeException :無法啓動活動ComponentInfo {com.example.fyp_awais.tab/com.example.fyp_awais.tab.MainActivity}:android.view.InflateException:二進制XML文件行#20:在android中錯誤擴展類片段 。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access $ 800(ActivityThread.java:151) at android.app .ActivityThread $ hhandleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app .ActivityThread.main(ActivityThread.java:5254) 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:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:android.view.InflateException:二進制XML文件行#20:錯誤的擴展類片段 在android.view.LayoutInflater.createViewFromTag( LayoutInflater.java:763) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在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:378) at andro id.app.Activity.setContentView(Activity.java:2145) at com.example.fyp_awais.tab.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:5990) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread。Java的:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(活套。 java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 由java導致:java .lang.NullPointerException at java.lang.VMClassLoader.findLoadedClass(Native Method) 在java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362) 在java.lang.ClassLoader.loadClass(ClassLoader.java:499) 在java.lang.ClassLoader.loadClass(ClassLoader.java:469) 在android.app.Fragment.instantiate(Fragment.java:604) 在android.app.Fragment.instantiate(Fragment.java:582) 在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108) 在機器人。 app.Activity.onCreateView(Activity.java:5328) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) at android.view。 LayoutInflater.rInflate(LayoutInflater.java:809) 在android.view.LayoutInflater.inflate(LayoutInflater.java:504) 在android.view.LayoutInflater.inflate(LayoutInflater.java:414) 在android.view.LayoutInflater.inflate(LayoutInflater.java:365) 在com.example.fyp_awais.tab.MainActivity.onCreate()中的com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) (android.app.Activity.setContentView(Activity.java:2145) MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access $ 800(ActivityThread.java:151) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303 )Android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 在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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 應用程序已終止。
發佈您的錯誤日誌。 – Noorul
@ user6750923,根據你的代碼片段,我找不到任何xml文件中的phone_container,你可以更新主要活動的xml – TejaDroid
@TejaDroid更新 – user6750923