gpt4 book ai didi

java - 即使在对象已初始化后仍为空对象引用

转载 作者:行者123 更新时间:2023-12-02 03:12:57 25 4
gpt4 key购买 nike

我正在尝试尽最大努力为我正在尝试创建的这个特定项目使用 fragment 。我正在我创建的 fragment 类中进行所有网络调用。当我调用建立连接的方法并将结果记录到控制台时,我从类中获取所有信息,但是当我尝试访问网络调用之外的实例时,我收到空指针异常。我一直在查看文档和其他示例,但其中大多数的做法与我需要的不同,并且文档没有提及在 fragment 内使用 okhttp

我正在使用的代码是这样的:

public void testAsyncCreate() {

OkHttpClient client = new OkHttpClient();


RequestBody body = RequestBody.create(CREATE_MEDIA_TYPE, "data={\"process\":\"123\", \"phone\":\"111111111\"}");


Request request = new Request.Builder()
.url(ALLWAYS_API)
.post(body)
.build();

Call call = client.newCall(request);

call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
// any additional runnable items go in here
try {


String jsonData = response.body().string(); // the exception is caught at this point
Log.d("<Mensajes>",jsonData);
//extractCreate = parseExtractCreateDetails(jsonData);
if(response.isSuccessful()) {
extractCreate = parseExtractCreateDetails(jsonData);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("<=====>", "check");
Log.d(TAG, "Is this even working?");
Log.d("The class has been ok",extractCreate.getCreateR().getAccount_id().toString());
Log.d("Another check", extractCreate.getStopsR()[1].getAddress());



}
});
}

//getStops(jsonData);

}catch(IOException e) {

}catch (JSONException e){

}


}
});
} // end of asyncCreate

然后在 onCreateView() 方法中:

ExtractCreate extractCreate = new ExtractCreate();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragments_stops_list, container, false);
ButterKnife.bind(this, view);

if (savedInstanceState == null) {
testAsyncCreate();
}
Log.d("<extractCreateCheck>", String.valueOf(extractCreate.getCreateR().getAccount_id()));

return view:
}

我不明白为什么这在使用 fragment 时不起作用,我这么说的原因是因为如果我将其放入常规 Activity 中并运行它,我将能够在其他任何地方使用 extractCreate 的结果想要,但不在这里。关于在 fragment 内使用 okhttp 调用,我是否缺少任何步骤?这些文档目前对我来说没有任何意义。堆栈跟踪如下:

11-23 09:28:55.569 3288-3288/com.example.testC.demotrackertree E/AndroidRuntime: FATAL EXCEPTION: main                                                                                   
Process: com.example.testC.demotrackertree, PID: 11142
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String at com.example.testC.demotrackertree.models.createmodels.CreateResponse.getAccount_id()' on a null object reference
at com.example.testC.demotrackertree.ui.fragments.StopCreateFragment.onCreateView(StopCreateFragment.java:95)
at android.app.Fragment.performCreateView(Fragment.java:2353)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.BackStackRecord.run(BackStackRecord.java:816)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

正如其中一个人所建议的,我在响应中做了我想做的事情,

if(response.isSuccessful()) {
extractCreate = parseExtractCreateDetails(jsonData);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Log.d("<=====>", "check");
// Log.d(TAG, "Is this even working?");
// Log.d("The class has been ok",extractCreate.getCreateR().getAccount_id().toString());
// Log.d("Another check", extractCreate.getStopsR()[1].getAddress());
Parcelable[] parcelable = extractCreate.getStopsR();

commodities = Arrays.copyOf(parcelable, parcelable.length, Stops[].class);
//Log.d("Commodities size check", String.valueOf(commodities.length));


StopsAdapter adapter = new StopsAdapter(extractCreate.getStopsR());
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); // sketchy on this part
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
Log.d(TAG, "This is being called from the onCreate method");
//();



}
});
}

//getStops(jsonData);

}catch(IOException e) {

}catch (JSONException e){

}


}

现在, View 的内容按照我在适配器中指定的方式进行相应更新(意味着适配器按我的预期运行),但在回调完成堆栈跟踪打印之前不会添加适配器

11-23 12:05:40.628 7270-7270/com.example.testC.demotrackertree E/RecyclerView: No adapter attached; skipping layout

不仅如此,当我旋转屏幕时,saveInstanceState 也不会被保留。

最佳答案

当您执行 AsyncTask 时,该操作与 UI 线程同时在后台线程上运行。 Log语句紧接在execute语句之后,因此当Log语句执行时,AsyncTask还没有加载变量中的信息。

关于java - 即使在对象已初始化后仍为空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40771450/

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