Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有时,在UIThread上访问带有@ViewById注释的字段时,返回null_Java_Android_Android Annotations_Findviewbyid_Android Runonuithread - Fatal编程技术网

Java 有时,在UIThread上访问带有@ViewById注释的字段时,返回null

Java 有时,在UIThread上访问带有@ViewById注释的字段时,返回null,java,android,android-annotations,findviewbyid,android-runonuithread,Java,Android,Android Annotations,Findviewbyid,Android Runonuithread,我有一个类使用了android注释和@ViewById注释。我见过很多崩溃,因为带有@ViewById注释的一些字段为空 尝试对空对象引用调用虚拟方法“void com.orangegangsters.github.swipreFreshLayout.library.swipreFreshLayout.setRefreshing(布尔)” 其中两个的问题 这是课程的一部分: @EFragment(R.layout.fragment_categories_tab) public class Cat

我有一个类使用了
android注释
@ViewById
注释。我见过很多崩溃,因为带有
@ViewById
注释的一些字段为空

尝试对空对象引用调用虚拟方法“void com.orangegangsters.github.swipreFreshLayout.library.swipreFreshLayout.setRefreshing(布尔)” 其中两个的问题

这是课程的一部分:

@EFragment(R.layout.fragment_categories_tab)
public class CategoriesTabFragment extends BaseFragment implements CategoryAdapter.OnItemSelected, SwipyRefreshLayout.OnRefreshListener {
    @ViewById(R.id.swipeRefreshLayout)
    protected SwipyRefreshLayout swipeRefreshLayout;
    @ViewById(R.id.placeholder_textview)
    protected CustomTextView statusLabel;

    private void loadChildren() {
        AndroidUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true); //crash happens here, but not always
            }
        });
    }

    categoryService.getCategories(new CommListener<Pair<Category[], Boolean>>() {
            @Override
            public void onSuccess(NetworkResult result, Pair<Category[], Boolean> object) {
                AndroidUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (getActivity() != null) {
                            statusLabel.setText(""); //another point that crash happens, but again sometime and only in early stage of the fragment's life
                        }
                    }
            }
    }
}

我是Android新手;我似乎没有找到原因。如果您能提前提供帮助,我将不胜感激。

AndroidUtil.runOnUiThread
更改为getActivity(),并在使用前检查null


在您的情况下,它会崩溃,因为有时您的片段没有连接到活动,所以您在其中的所有布局都是空的。

这是因为您在片段调用之前调用了片段onCreateView@Selvin“我使用AndroidAnnotations,从@AfterView AfterView()方法及其后调用所有内容。片段不是在这些时间点附加到活动上吗?1.将其更改为getActivity()是什么意思?2.所有这些方法都在@AfterView AfterView()方法中被调用,这不意味着此时片段已附加到活动?您正在进行异步调用,因此响应可能会延迟,在这段时间内,您的片段可能未附加到活动,因此视图对象将为空。
public class AndroidUtil {
    private static Handler mainHandler = new Handler(Looper.getMainLooper());

    public static void runOnUiThread(Runnable runnable) {
        mainHandler.post(runnable);
    }
}