加载静态片段和FrameLayout时出错(

时间:2015-11-02 21:19:28

标签: android android-fragments layout static

我正在尝试使用此布局加载带有静态片段和LinearLayout的布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    tools:context="com.eldad.yossi.popularmovs.MainActivity">
    <fragment
        android:id="@+id/main_fragment"
        android:name="com.eldad.yossi.popularmovs.MainActivityFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@android:layout/list_content" />
    <FrameLayout
        android:id="@+id/movies_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4" />
</LinearLayout>

但是当应用程序启动时,我收到此错误:

11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #2
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime: Process: com.eldad.yossi.popularmovs, PID: 13809
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:142)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1437)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:462)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompatJellybean.query(ContentResolverCompatJellybean.java:29)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat$ContentResolverCompatImplJB.query(ContentResolverCompat.java:57)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat.query(ContentResolverCompat.java:125)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:59)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:37)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:296)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:54)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:42)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:128)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
11-02 23:02:36.418 13809-13880/com.eldad.yossi.popularmovs E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 

当我使用仅包含片段的布局加载它们时,相同的Activity和片段都有效。

任何想法?

完整代码位于GitHub仓库中 - https://github.com/yossildad/PopularMovs 感谢。

3 个答案:

答案 0 :(得分:0)

从堆栈跟踪中我可以看到您正在尝试从活动com.eldad.yossi.popularmovs.MainActivity中的doInBackground()方法中的空引用中获取URI。

答案 1 :(得分:0)

好的,我发现了这个错误。我在创建Loader时使用的是未初始化的uri 在片段里面。 感谢您的帮助,并为此事感到抱歉。

答案 2 :(得分:-1)

您的问题不在片段声明中。根据stacktrace,查看您扩展的ModernASyncTask的doInBackground方法。 java.lang.RuntimeException:执行doInBackground()时发生错误 - 我猜你可能对上下文对象有空引用。