gpt4 book ai didi

java - Android: NullPointerException: println 需要一条消息;

转载 作者:行者123 更新时间:2023-12-01 18:19:06 26 4
gpt4 key购买 nike

我正在尝试使用 fragment 构建一个具有主布局和详细布局的应用程序,数据来自通过使用 Asynctask 使用 Web 服务来解析 JSOnArray。

无论我按照建议对各个论坛上的类似问题应用什么修复程序,我总是从 logcat 中看到此错误:

FATAL EXCEPTION: main
Process: com.example.masterdetail, PID: 26639
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at com.example.masterdetail.ItemsListFragment.populateResult(ItemsListFragment.java:108) //this is the line where I am building an arraylist in ItemsListFragment.java
at com.example.masterdetail.AsyncRequest.onPostExecute(AsyncRequest.java:147)
at com.example.masterdetail.AsyncRequest.onPostExecute(AsyncRequest.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

==================ItemsListFragment.java:

public void populateResult(String response) {
/*TextView resultView = (TextView)getActivity().findViewById(R.id.textUrlContent);
resultView.setText(response);*/
try {
ListView lvItems = (ListView) getActivity().findViewById(R.id.lvItems);
ArrayList<Item> products = new ArrayList<Item>();
JSONObject jsonObject = new JSONObject(response);
JSONObject responseObj = jsonObject.getJSONObject("response");
JSONArray jsonDataProducts = responseObj.getJSONArray("products");

for (int i = 0; i < jsonDataProducts.length(); i++) {
JSONObject oneObject = jsonDataProducts.getJSONObject(i);
Item dataProduct = new Item();
dataProduct.setTitle(oneObject.getString("title"));
dataProduct.setBody(oneObject.getString("id"));
products.add(dataProduct); //***Line: 108 from above error msg ************
}

adapterItems = new ArrayAdapter<Item>(getActivity(),
android.R.layout.simple_list_item_activated_1, products);
lvItems.setAdapter(adapterItems);
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View item, int position,
long rowId) {
// Retrieve item based on position
Item i = adapterItems.getItem(position);
// Fire selected event for item
listener.onItemSelected(i);
}
});
} catch (Exception e) {
Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage());
}
}

最佳答案

Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage());,

e.getlocalizedMessage() 返回 null。打电话前先检查一下。

Log.d("ReadRdaJSONFeedTask", e.getLocalizedMessage() == null ? "" : e.getLocalizedMessage());

关于java - Android: NullPointerException: println 需要一条消息;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28078781/

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