Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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
Android findViewById仅在第二次启动时为空-碎片-截击HTTP_Android_Android Fragments_Android Activity_Findviewbyid - Fatal编程技术网

Android findViewById仅在第二次启动时为空-碎片-截击HTTP

Android findViewById仅在第二次启动时为空-碎片-截击HTTP,android,android-fragments,android-activity,findviewbyid,Android,Android Fragments,Android Activity,Findviewbyid,我有一个带有碎片的应用程序 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view=inflater.inflate(R.layout.my_list_view, container, fa

我有一个带有碎片的应用程序

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    view=inflater.inflate(R.layout.my_list_view, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    RequestManager.getInstance(getActivity()).doRequest().Populate();
    // This is Android Volley HTTP Request Manager (using RequestProxy.java)
    // Inside Populate() I use activity.findViewById(R.id.myList), which is the list in the xml.
    // activity is MainActivity I passed to the RequestManager above.
}
更新:添加填充方法

} 安装后我第一次运行应用程序时,它工作得非常完美。 然后,如果我通过按back按钮finish退出并再次启动应用程序,findViewByIdR.id.myList将返回null

仅当我使用android.os.Process.killProcessandroid.os.Process.myPid时;在onDestroy方法中,应用程序将在下次正确启动

事实:

onCreateView在我第二次启动应用程序时被正确调用。 视图第二次也正确充气。 如果我将onActivityCreated中的findViewByIdR.id.myList设置为空。只有当我从Populate调用它时,它才会返回null。 所以

为什么第一次有效,第二次无效

为什么在“填充”中第二次丢失活动引用

更新:经过一些调试,我发现活动引用并没有丢失。我不能只是引用片段的视图

如何让Popuplate从外部类引用片段上的视图。 类似MainActivity.fragment1.view的内容


旁注:我使用了不止一个片段,我只是在这里总结一下想法。

我知道这是很久以前的事了,, 但我也面临同样的问题,下面的文章作为一种魅力解决了这个问题


你也必须发布日志!!我不想对日志造成更多的混淆。这更像是一个概念问题。安卓系统的生命周期让它第二次出错。甚至可能是一只虫子。我需要你的经验来解开这个谜团。但是我们不能帮助你,因为你没有发布日志!我没有得到错误,因为我验证了空对象的情况,但是我在第二次运行应用程序时得到了一个空视图。这很奇怪。你可以把剩下的代码发出去吗?
public class RequestProxy {
    public Activity activity;

    public void Populate(){
        JsonArrayRequest JsonRequest = new JsonArrayRequest(feed_url, new Response.Listener<JSONArray> () {
            @Override
            public void onResponse(JSONArray aJsonResponse) {

                // -----------
                // ... I work here with the response to match format, etc...
                // -----------

                // Populate list with response
                CustomAdapter listAdapter = new CustomAdapter(activity,MyArray,1);
                ListView myList = (ListView) activity.findViewById(R.id.myList);
                Log.e("myList is Null", String.valueOf(myList == null)); // Null the second time, not the first.
                if(myList!=null) myList.setAdapter(newsAdapter);
            }
        }, new Response.ErrorListener() {
            // Irrelevant...
        });
    }
}