在我的程序中,我有一个listview,它显示来自SQL数据库的各种客户信息,以及一个清除数据库中所有客户条目的按钮。我的问题是:
我的主要计划:
package attendance.emily.example.com.attendance;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Button cleardata;
TextView textview3, displayUser, displayTimeout, displayTimein, textview7;
ListView lv;
ArrayAdapter<String> adapter;
String address = "http://192.168.0.100:80/gps/get_logout.php";
InputStream is = null;
String line = null;
String result = null;
String data[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
cleardata = (Button) findViewById(R.id.button);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
getdata3();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
lv.setAdapter(adapter);
cleardata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String link = "http://192.168.0.100:80/gps/delete.php"; //using this IP for Genymotion emulator
new updateData().execute(link);
Toast.makeText(MainActivity.this, "Executed", Toast.LENGTH_LONG).show();
}
});
}
public class updateData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection conn = null;
try {
URL url;
url = new URL(params[0]);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
} else {
InputStream err = conn.getErrorStream();
}
return "Done";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
}
private void getdata3()
{
try{
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
is = new BufferedInputStream(con.getInputStream());
} catch(Exception e){
e.printStackTrace();
}
try{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
while ((line=br.readLine()) != null){
sb.append(line+"\n");
}
is.close();
result = sb.toString();
} catch(Exception e){
e.printStackTrace();
}
try {
String a="";
String p="";
String z="";
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
data=new String[ja.length()];
for (int i=0; i<ja.length(); i++ ){
jo = ja.getJSONObject(i);
a =jo.getString("user");
p = jo.getString("MIN(timein)");
z = jo.getString("MAX(timeout)");
data[i] = "User: "+ a + "\n" +"Time In: " + p + "\n" + "Time Out: " + z+"\n" ;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: attendance.emily.example.com.attendance, PID: 11833
java.lang.RuntimeException: Unable to start activity ComponentInfo{attendance.emily.example.com.attendance/attendance.emily.example.com.attendance.MainActivity}: java.lang.NullPointerException: storage == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at attendance.emily.example.com.attendance.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
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:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
关于如何解决我的问题的任何建议?
答案 0 :(得分:0)
在致电adapter.notifyDataSetChanged();
之前,必须更新adapter
(或其拥有的数据),如下所示:
String s = "new item";
adapter.add(s);
然后
adapter.notifyDataSetChanged();
然而,更好的方法:
由于您使用的是String[]
,因此无法向其中添加项目。我建议使用ArrayList<String> list = new ArrayList<String>();
,然后像现在一样使用它填充适配器。
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
然后,当您想要添加新项目时,请执行以下操作:
list.add(s);
adapter.notifyDataSetChanged();
因此,如果要显示新的列表项,请先更新列表,然后调用notifyDataSetChanged()
。旁注:您也可以以相同的方式更新现有行,但您必须获得该位置,通常从onClick
方法传入。