我在片段XML有這樣的:我的活動崩潰的一個奇怪的原因
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/sphereIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rsz_rsz_mystic"
android:contentDescription="@string/magicBallDescr"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
,這裏是我的onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview;
webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://www.google.com");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
一切似乎沒什麼問題,但是當我運行的應用程序崩潰了很多錯誤。
這裏是日誌:
04-26 07:27:22.818: E/AndroidRuntime(1834): FATAL EXCEPTION: main
04-26 07:27:22.818: E/AndroidRuntime(1834): Process: com.gelasoft.answeringball, PID: 1834
04-26 07:27:22.818: E/AndroidRuntime(1834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gelasoft.answeringball/com.gelasoft.answeringball.MainActivity}: java.lang.NullPointerException
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.os.Looper.loop(Looper.java:136)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-26 07:27:22.818: E/AndroidRuntime(1834): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 07:27:22.818: E/AndroidRuntime(1834): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-26 07:27:22.818: E/AndroidRuntime(1834): at dalvik.system.NativeStart.main(Native Method)
04-26 07:27:22.818: E/AndroidRuntime(1834): Caused by: java.lang.NullPointerException
04-26 07:27:22.818: E/AndroidRuntime(1834): at com.gelasoft.answeringball.MainActivity.onCreate(MainActivity.java:24)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.Activity.performCreate(Activity.java:5231)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 07:27:22.818: E/AndroidRuntime(1834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-26 07:27:22.818: E/AndroidRuntime(1834): ... 11 more
缺少什麼我在這裏?我知道它確實很小,但我無法找到它。
MainActivity的第24行是什麼? – Ran
@Ran'webview.setWebViewClient(new WebViewClient());' – chility