我访问了很多这样的类似链接,例如1,2跟随各种示例/链接,即this one和this one,主要是我从{{{ 3}}太多次了,每当我得到同样的例外......可能是什么原因?
07-27 12:00:33.431: E/AndroidRuntime(663): FATAL EXCEPTION: main
07-27 12:00:33.431: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myexample.fragmentdemoexample/com.myexample.fragmentsupportexample.MainFragmentActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:137)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 12:00:33.431: E/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 12:00:33.431: E/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 12:00:33.431: E/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method)
07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-27 12:00:33.431: E/AndroidRuntime(663): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Activity.setContentView(Activity.java:1835)
07-27 12:00:33.431: E/AndroidRuntime(663): at com.myexample.fragmentsupportexample.MainFragmentActivity.onCreate(MainFragmentActivity.java:14)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Activity.performCreate(Activity.java:4465)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-27 12:00:33.431: E/AndroidRuntime(663): ... 11 more
07-27 12:00:33.431: E/AndroidRuntime(663): Caused by: java.lang.ClassCastException: com.myexample.fragmentdemoexample.MyListFragment cannot be cast to android.support.v4.app.Fragment
07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
07-27 12:00:33.431: E/AndroidRuntime(663): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-27 12:00:33.431: E/AndroidRuntime(663): ... 21 more
编辑这是我的完整代码 // MainActivity
public class MainFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_fragment, menu);
return true;
}
}
// DetailActivity
public class DetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Need to check if Activity has been switched to landscape mode
// If yes, finished and go back to the s tart Activity
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.details_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String s = extras.getString("value");
TextView view = (TextView) findViewById(R.id.detailsText);
view.setText(s);
}
}
}
// MyListFragment
public class MyListFragment extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) (getActivity().getSupportFragmentManager()).findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(item);
} else {
Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class);
intent.putExtra("value", item);
startActivity(intent);
}
}
}
// DetailsFragment
public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
//布局是 //activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.myexample.fragmentdemoexample.MyListFragment" >
</fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
// details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</LinearLayout>
和layout-port的代码类似于Vogella的帖子中提到的相应更改
答案 0 :(得分:1)
我就是这样做的:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.Button;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class MainPoP extends SherlockFragmentActivity implements ListFragment.OnMainMenuListener{
Button home;
Button about;
Button donate;
Button explore;
ActionBar actionbar;
ListFragment mainMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
mainMenu = new ListFragment();
ft.add(R.id.mainView, mainMenu).addToBackStack(null).commit();
}
}
这就是我制作片段的方式,它的工作正常。尝试使用OnCreateView方法在扩展Fragment的类中像这样膨胀你的片段,或者是你想要设置一些布局的特定片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.about_fragment, container, false);
return v;
}
答案 1 :(得分:1)
所有代码看起来都很好,请记住,你有两个xml文件activity_main.xml,一个将在layout文件夹中,第二个将在layout-port文件夹中。
有关Vogella的完整工作演示,请查看此链接项目。