【问题标题】:My activity is crashing for a strange reason我的活动因奇怪的原因而崩溃
【发布时间】:2014-04-26 11:31:02
【问题描述】:

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

我在这里缺少什么?我知道它很小,但我找不到。

【问题讨论】:

  • MainActivity 的第 24 行是什么?
  • @Ran webview.setWebViewClient(new WebViewClient());

标签: java android android-activity android-webview android-fragmentactivity


【解决方案1】:

试试这个..

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();
}

【讨论】:

  • @chility 很高兴它有帮助。快乐编码。
【解决方案2】:

布局 XML 用于您 Activity 中的 Fragment,这就是 findViewById() 找不到您的视图并返回 null 的原因。

将代码移到 PlaceHolderFragment 内部类或 WebView xml 元素到 activity_main.xml。

当您使用 eclipse 时,它​​会在向导创建的每个新 Activity 上添加一个新片段。这个我个人觉得不对,但是我知道Android团队是在努力推动大家到处使用fragment..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多