我已经在stackoverflow上阅读了很多关于此异常的内容(如果您出于同样的原因,可以检查提示this,this或this)。我的应用程序在纵向模式下表现正常,但在横向上我得到此错误。根据其他帖子,原因可能会有所不同。一种流行的方法是使用Support Library
,但我不确定这是自我minSDK=15
以来的解决方案。
我的踪迹
3799-3799 E: FATAL EXCEPTION: main
3799-3799 E: Process: com.githublist1, PID: 3799
3799-3799 E: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.githublist1/com.githublist1.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
3799-3799 E: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
3799-3799 E: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
3799-3799 E: at android.app.ActivityThread.access$800(ActivityThread.java:135)
3799-3799 E: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
3799-3799 E: at android.os.Handler.dispatchMessage(Handler.java:102)
3799-3799 E: at android.os.Looper.loop(Looper.java:136)
3799-3799 E: at android.app.ActivityThread.main(ActivityThread.java:5001)
3799-3799 E: at java.lang.reflect.Method.invokeNative(Native Method)
3799-3799 E: at java.lang.reflect.Method.invoke(Method.java:515)
3799-3799 E: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
3799-3799 E: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
3799-3799 E: at dalvik.system.NativeStart.main(Native Method)
3799-3799 E: Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
3799-3799 E: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
3799-3799 E: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
3799-3799 E: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
3799-3799 E: at android.app.Activity.setContentView(Activity.java:1929)
3799-3799 E: at com.githublist1.MainActivity.onCreate(MainActivity.java:42)
3799-3799 E: at android.app.Activity.performCreate(Activity.java:5231)
3799-3799 E: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
3799-3799 E: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
3799-3799 E: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
3799-3799 E: at android.app.ActivityThread.access$800(ActivityThread.java:135)
3799-3799 E: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
3799-3799 E: at android.os.Handler.dispatchMessage(Handler.java:102)
3799-3799 E: at android.os.Looper.loop(Looper.java:136)
3799-3799 E: at android.app.ActivityThread.main(ActivityThread.java:5001)
3799-3799 E: at java.lang.reflect.Method.invokeNative(Native Method)
3799-3799 E: at java.lang.reflect.Method.invoke(Method.java:515)
3799-3799 E: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
3799-3799 E: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
3799-3799 E: at dalvik.system.NativeStart.main(Native Method)
3799-3799 E: Caused by: java.lang.NullPointerException
3799-3799 E: at com.githublist1.RepoListFragment.onCreate(RepoListFragment.java:64)
3799-3799 E: at android.app.Fragment.performCreate(Fragment.java:1678)
3799-3799 E: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
3799-3799 E: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
3799-3799 E: at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
3799-3799 E: at android.app.Activity.onCreateView(Activity.java:4786)
3799-3799 E: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
3799-3799 E: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
3799-3799 E: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
3799-3799 E: at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
3799-3799 E: at android.app.Activity.setContentView(Activity.java:1929)
3799-3799 E: at com.githublist1.MainActivity.onCreate(MainActivity.java:42)
3799-3799 E: at android.app.Activity.performCreate(Activity.java:5231)
3799-3799 E: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
3799-3799 E: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
3799-3799 E: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
3799-3799 E: at android.app.ActivityThread.access$800(ActivityThread.java:135)
3799-3799 E: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
3799-3799 E: at android.os.Handler.dispatchMessage(Handler.java:102)
3799-3799 E: at android.os.Looper.loop(Looper.java:136)
3799-3799 E: at android.app.ActivityThread.main(ActivityThread.java:5001)
3799-3799 E: at java.lang.reflect.Method.invokeNative(Native Method)
3799-3799 E: at java.lang.reflect.Method.invoke(Method.java:515)
3799-3799 E: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
3799-3799 E: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
3799-3799 E: at dalvik.system.NativeStart.main(Native Method)
activity_main.xml 肖像
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
activity_main.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="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.githublist1.RepoListFragment"
android:id="@+id/fragment_list"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_repolist_list"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
</fragment>
<fragment
android:name="com.githublist1.RepoDetailFragment"
android:id="@+id/fragment_detail"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_repolist_detail">
</fragment>
</LinearLayout>
MainActivity
public class MainActivity extends Activity{
static List<Repository> repositoriesList;
RepositoryDataSource repoDS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//// TODO: 25.11.2015 Delete this part after async impl
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
retriveRepo();
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) != null) {
RepoListFragment firstFragment = new RepoListFragment();
// Configure transaction
Bundle args = new Bundle();
args.putParcelableArrayList(RepoListFragment.ARG_PARAM1, (ArrayList<? extends Parcelable>) repositoriesList);
firstFragment.setArguments(args);
FragmentManager fragMan = getFragmentManager();
FragmentTransaction transaction = fragMan.beginTransaction();
transaction.add(R.id.fragment_container, firstFragment);
// Commit the transaction
transaction.commit();
}
}
void onRepositorySelected(int position) {
// Capture the article fragment from the activity layout
RepoDetailFragment detailFragment = (RepoDetailFragment)
getFragmentManager().findFragmentById(R.id.fragment_detail);
Repository repositorySelected = repositoriesList.get(position);
Log.i("onRepositorySelected",repositorySelected.toString());
if (detailFragment != null) {
// If article frag is available - in two-pane layout...
// Call a method in the RepoDetailFragment to update its content
detailFragment.updateDetailsView(repositorySelected);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
RepoDetailFragment newFragment = new RepoDetailFragment();
Bundle args = new Bundle(); // Transports position to newly created fragment
args.putParcelable(RepoDetailFragment.REPOSITORY_SELECTED, repositorySelected);
newFragment.setArguments(args);
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
RepoListFragment
public class RepoListFragment extends ListFragment {
static final String ARG_PARAM1 = "param1";
private ArrayList<Repository> repolist;
MainActivity mCallback;
private GitHubAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {/////////////////////////////////////////////////////////////////////////////
super.onCreate(savedInstanceState);
//// TODO: 28.11.2015 check it!
repolist = getArguments().getParcelableArrayList(ARG_PARAM1);
// TODO: Change Adapter to display your content
for (Repository repository : repolist) {
Log.i("Get in fragment", repository.toString());
}
mAdapter = new GitHubAdapter(getActivity(), repolist);
// Set the adapter
setListAdapter(mAdapter);
}
@Override
public void onStart() {////////////////////////////////////////////////////////////////////////////////////////////////
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.fragment_detail) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallback = (MainActivity) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_repolist, container, false);
return view;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onRepositorySelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
RepoDetailFragment
public class RepoDetailFragment extends Fragment {
final static String REPOSITORY_SELECTED = "repositoryDetails";
Repository currentRepository = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
currentRepository = savedInstanceState.getParcelable(REPOSITORY_SELECTED);
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_repolist_detail, container, false);
}
@Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateDetailsView((Repository) args.getParcelable(REPOSITORY_SELECTED));
} else if (currentRepository != null) {
// Set article based on saved instance state defined during onCreateView
updateDetailsView(currentRepository);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void updateDetailsView(Repository repository) {
currentRepository = repository;
TextView fullName = (TextView) getActivity().findViewById(R.id.full_name);
TextView description = (TextView) getActivity().findViewById(R.id.description);
TextView language = (TextView) getActivity().findViewById(R.id.language);
TextView url = (TextView) getActivity().findViewById(R.id.url);
fullName.setText(repository.getFull_name());
description.setText(repository.getDescription());
language.setText(repository.getLanguage());
url.setText(repository.getHtml_url());
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current repo selection in case we need to recreate the fragment
outState.putParcelable(REPOSITORY_SELECTED, currentRepository);
}
答案 0 :(得分:0)
问题是试图在getActivity().findViewById(...)
的帮助下获取视图。我使用了基于支持库的代码示例,它没有getView()
方法。使用getView()
解决了问题。