我正在创建一个有两个方向的Android应用程序。我可以在风景中打开并切换到肖像,但如果我尝试重新回到横向,应用程序崩溃。纵向活动类扩展了FragmentActivity,而横向类扩展了Activity。任何建议将不胜感激。提前谢谢。
MainActivity.java(旧):
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_main);
return;
}
startActivity(new Intent(this, PortActivity.class));
}
}
MainActivity.java(新):
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
PortActivity.java(new OnCreate):
public class PortActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;//The {@link ViewPager} that will host the section contents.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.activity_port);
etc(same as before)....
PortActivity.java(旧的OnCreate):
public class PortActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;//The {@link ViewPager} that will host the section contents.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_port);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_port_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
的Manifest.xml:
<activity
android:name="com.example.mathapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.mathapp.PortActivity"
android:label="@string/app_name" >
</activity>
日志文件:
02-17 14:29:53.704: W/ResourceType(3223): Failure getting entry for 0x7f030001 (t=2 e=1)
in package 0 (error -75)
02-17 14:29:53.714: D/AndroidRuntime(3223): Shutting down VM
02-17 14:29:53.724: W/dalvikvm(3223): threadid=1: thread exiting with uncaught exception (group=0xb501c180)
02-17 14:29:53.774: E/AndroidRuntime(3223): FATAL EXCEPTION: main
02-17 14:29:53.774: E/AndroidRuntime(3223): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathapp/com.example.mathapp.PortActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.access$700(ActivityThread.java:123)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.os.Looper.loop(Looper.java:137)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-17 14:29:53.774: E/AndroidRuntime(3223): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 14:29:53.774: E/AndroidRuntime(3223): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 14:29:53.774: E/AndroidRuntime(3223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-17 14:29:53.774: E/AndroidRuntime(3223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-17 14:29:53.774: E/AndroidRuntime(3223): at dalvik.system.NativeStart.main(Native Method)
02-17 14:29:53.774: E/AndroidRuntime(3223): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.content.res.Resources.getValue(Resources.java:1018)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.content.res.Resources.getLayout(Resources.java:857)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-17 14:29:53.774: E/AndroidRuntime(3223): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.Activity.setContentView(Activity.java:1835)
02-17 14:29:53.774: E/AndroidRuntime(3223): at com.example.mathapp.PortActivity.onCreate(PortActivity.java:50)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.Activity.performCreate(Activity.java:4465)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-17 14:29:53.774: E/AndroidRuntime(3223): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-17 14:29:53.774: E/AndroidRuntime(3223): ... 12 more