gpt4 book ai didi

java - 我的 Activity 因奇怪的原因而崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:09 25 4
gpt4 key购买 nike

我的 fragment 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

我在这里错过了什么?我知道它真的很小,但我找不到它。

最佳答案

试试这个..

PlaceholderFragment.java 中执行您的 WebView 初始化和其他代码。因为 WebViewfragment.xml

或者

setContentView(R.layout.activity_main); 更改为 setContentView(R.layout.fragment); 并删除

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

关于java - 我的 Activity 因奇怪的原因而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23310221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com