我想在tablet视图中显示列表+描述。下面的代码在纵向模式下运行良好,但在横向模式下停止运行。有什么不对劲吗?
主要活动
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/layout-land/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:无法启动activity组件信息{com.example.fyp_awais.tab/com.example.fyp_awais.tab.MainActivity}:Android.view.inflateException:二进制XML文件行#20:在Android.app.activityThread.PerformLaunchActivity(ActivityThread.java:2325)在Android.app.activityThread.Access$800(ActivityThread.java:2325)Java:698)原因:Android.View.Inflater.Exception:二进制XML文件行#20:在Android.View.LayoutInflater.CreateViewFromTag(LayoutInflater.Java:763)在Android.View.LayoutInflater.rInflate(LayoutInflater.Java:806)在Android.View.LayoutInflater.rInflate(LayoutInflater.Java:809)在Ad.java:2387)Android.app.ActivityThread.Access $800(ActivityThread.java:151)Android.app.ActivityThread $H.HandleMessage(ActivityThread.java:1303) Android.os.Handler.DispatchMessage(ActivityThread.java:102) Android.os.Looper.Loop(ActivityThread.java:135) Android.app.ActivityThread.Main(ActivityThread.java:5254) java.lang.Reflect.Method.Invoke(原生VA:582)在Android.app.FragmentManager.java:2108)在Android.app.activity.onCreateView(activity.java:5328)在Android.view.LayoutInflater.CreateViewFromTag(LayoutInflater.java:733)在Android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 在Android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在y(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(looper.java:135) 在android.app.activityThread.main(activityThread.java:5254) 在java.lang.Reflect.Method.Invoke(原生方法) 在java.lang.Reflect.Method.Invoke(method.java:372) 在com.android.internal.os.zygoteInit$methodandargscaller.run(zygoteInit.java:903) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 应用程序已终止。
你确定你在横向模式中有错误吗?因为在Landage,its运行精细,而在肖像模式下,您没有使用动态片段,所以您需要扩展片段的activity,而不是activity来运行片段。如果您使用动态片段而不是硬编码,这将是很好的。
将layout-port/main_activity更改为
<?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"
android:id="@+id/phone_container2"
>
// remove hard coded fragment
</FrameLayout>
//您的完整解决方案
主activity
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
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;
//if's the first time created
MyDetailFragment myListFragment = new MyDetailFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.phone_container2, myListFragment);
fragmentTransaction.commit();
/*
* MyListFragment and MyDetailFragment have been loaded in XML,
* no need load.
*/
}else{
//it's run on phone
//Load MyListFragment programmatically
isSinglePane = true;
//if's the first time created
MyListFragment myListFragment = new MyListFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.phone_container, myListFragment);
fragmentTransaction.commit();
}
}
}
端口:
<?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"
android:id="@+id/phone_container2"
>
<!-- <fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>-->
</FrameLayout>
土地:
<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>
我想添加一个对象LinearLayout,但它抛出一个错误错误行:在me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)(addElement(uris.get(i),names.get(i));) 新闻片段 fragment_news: mews_新闻。xml: 错误: E/Androi
这里的主要问题是:上面的代码在几乎所有的设备(受刺激的设备,或者一些真实的设备)上都能顺利运行。但当我在三星S3上运行时。它注意到这个错误: 请告诉我如何修复错误,谢谢:)
这里是MainActivity类 我的LogCat文件是 求你了,救命
当我点击后退按钮并返回到位置片段时,我遇到了一些问题,下面出现了一个错误: 我确实查了很多帖子,尝试了所有的解决方案,但它不起作用。似乎其他菜单项不是问题,只是这个位置页面给出了一个错误。 fragment_location.xml LocationFragment.java
致命异常:java.lang.RuntimeException:无法启动activity ComponentInfo{com.test/com.Activities.ViewActivity}:Android.View.InflateException:二进制XML文件行#29:二进制XML文件行#29:在Android.app.ActivityThread.HandleLaunchActivit