我正在尝试使用具有两种不同布局的片段开发一个应用程序,类似于这个:
当我在手机上运行我的应用程序时,它会立即崩溃。这是我的代码:
/layout
activity_titles_fragment.xml
details_fragment.xml
/layout-land
activity_titles_fragment.xml
/sec
DetailsActivity.java
DetailsFragment.java
TitlesFragment.java
activity_titles_fragment.xml
<FrameLayout 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"
tools:context=".TitlesFragment" >
<fragment
class="com.example.fragmentsapp.TitlesFragment"
android:id="@+id/titles"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
details_fragment.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"
android:orientation="vertical" >
<TextView
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
对于景观,activity_titles_fragment.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
class="com.example.fragmentsapp.TitlesFragment"
android:id="@+id/titles"
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/details"
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight="1" />
</LinearLayout>
详细信息Activity.java
package com.example.fragmentsapp;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class DetailsActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(
R.id.details, details).commit();
}
}
}
详细信息Fragment.java
package com.example.fragmentsapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailsFragment extends Fragment {
public static DetailsFragment newInstance(int index)
{
// supply input index as argument
DetailsFragment f = new DetailsFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex()
{
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null)
return null;
TextView text = new TextView(getActivity());
text.setText("Fragment number: " + getShownIndex());
return text;
}
}
TitlesFragment.java
package com.example.fragmentsapp;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// populate list items with list view
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.titles)));
// check for mobile dual pane
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null)
{
mCurCheckPosition = savedInstanceState.getInt("curChoice",0);
}
if (mDualPane)
{
showDetails(mCurCheckPosition);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
private void showDetails(int index) {
mCurCheckPosition = index;
if(mDualPane)
{
DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
if(details == null || details.getShownIndex() != index)
{
details = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}