gpt4 book ai didi

android - 动态惰性列表

转载 作者:行者123 更新时间:2023-11-29 15:31:06 25 4
gpt4 key购买 nike

大家好我想做this懒惰的动态。我首先尝试使用图像,但它会强制关闭。如果我的方法有误,请指导。这是代码

public class Test extends Activity {

ListView list;
LazyAdapter adapter;
private String[] mStrings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL(
"http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=123");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));

String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);

for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
listItems.add(jo.getString("http://midsweden.gofreeserve.com/proj/admin/pictures/file87619.jpg"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

mStrings = listItems.toArray(new String[listItems.size()]);
list=(ListView)findViewById(R.id.list);

adapter=new LazyAdapter(this, mStrings);
list.setAdapter(adapter);

Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(listener);
}
@Override
public void onDestroy()
{
adapter.imageLoader.stopThread();
list.setAdapter(null);
super.onDestroy();
}

public OnClickListener listener=new OnClickListener(){
public void onClick(View arg0) {
adapter.imageLoader.clearCache();
adapter.notifyDataSetChanged();
}
};




}

日志

07-09 01:00:51.825: WARN/dalvikvm(12674): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
07-09 01:00:51.835: ERROR/AndroidRuntime(12674): Uncaught handler: thread main exiting due to uncaught exception
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.os.Looper.loop(Looper.java:123)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread.main(ActivityThread.java:4595)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at dalvik.system.NativeStart.main(Native Method)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): Caused by: java.lang.NullPointerException
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at com.droidnova.android.howto.optionmenu.LazyAdapter.getCount(LazyAdapter.java:27)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.widget.ListView.setAdapter(ListView.java:431)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:35)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): ... 11 more
07-09 01:00:51.895: INFO/Process(82): Sending signal. PID: 12674 SIG: 3
07-09 01:00:51.895: INFO/dalvikvm(12674): threadid=7: reacting to signal 3

最佳答案

您的 NullPointer 异常在 getCount() 第 27 行:

Caused by: java.lang.NullPointerException
at com.droidnova.android.howto.optionmenu.LazyAdapter.getCount(LazyAdapter.java:27)

您可以通过将 getCount() 更改为以下内容来修复此症状:

public int getCount() {
if(data != null){
return data.length;
}
return 0;
}

关于android - 动态惰性列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631424/

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