当我旋转到横向并按下主要活动上的按钮时,应用程序崩溃。如果我按下纵向按钮,在次要活动中切换到横向,然后回到横向的主要活动,它不会崩溃。当我在横向按下该按钮时它会崩溃,我不知道它是如何或为什么这样做。
您可能需要查看其他任何文件吗?请让我知道,第一次在这里发帖。
这是按钮
的主要活动中的xml代码<Button
android:id="@+id/button_acknowledgements"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="@string/acknowledgments"
android:onClick="acknowledgments"
android:textColor="#FFFFFF"/>
这是mainactivity.java文件中创建新活动的方法
public void acknowledgments(View view){
Intent intent = new Intent(this, Acknowledgments.class);
startActivity(intent);
}
这就是activity_aknowledments.xml文件中的全部内容
<WebView
android:id="@+id/AckWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
这是Acknowledgments.java文件中的内容
package com.example.spacepowerexchangeordersubmission;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class Acknowledgments extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acknowledgments);
// Show the Up button in the action bar.
setupActionBar();
WebView web = (WebView) findViewById(R.id.AckWebView);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setLoadsImagesAutomatically(true);
web.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
web.getSettings().setDomStorageEnabled(true);
web.loadUrl("http://www.prism.gatech.edu/~rpanjwani3/");
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.acknowledgments, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}
我还在活动中的android清单中包含了android:configChanges =“keyboardHidden | orientation | screenSize”限定符
02-09 17:59:08.198: E/AndroidRuntime(22749): FATAL EXCEPTION: main
02-09 17:59:08.198: E/AndroidRuntime(22749): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spacepowerexchangeordersubmission/com.example.spacepowerexchangeordersubmission.Acknowledgments}: java.lang.NullPointerException`
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.os.Looper.loop(Looper.java:137)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-09 17:59:08.198: E/AndroidRuntime(22749): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 17:59:08.198: E/AndroidRuntime(22749): at java.lang.reflect.Method.invoke(Method.java:525)
02-09 17:59:08.198: E/AndroidRuntime(22749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-09 17:59:08.198: E/AndroidRuntime(22749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-09 17:59:08.198: E/AndroidRuntime(22749): at dalvik.system.NativeStart.main(Native Method)
02-09 17:59:08.198: E/AndroidRuntime(22749): Caused by: java.lang.NullPointerException
02-09 17:59:08.198: E/AndroidRuntime(22749): at com.example.spacepowerexchangeordersubmission.Acknowledgments.setupActionBar(Acknowledgments.java:35)
02-09 17:59:08.198: E/AndroidRuntime(22749): at com.example.spacepowerexchangeordersubmission.Acknowledgments.onCreate(Acknowledgments.java:19)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.Activity.performCreate(Activity.java:5372)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-09 17:59:08.198: E/AndroidRuntime(22749): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-09 17:59:08.198: E/AndroidRuntime(22749): ... 11 more