我是Android编程新手。我正在尝试从MYSQL数据库中检索数据。但我无法检索我的数据。我的代码是
public class News_Updates extends Activity implements View.OnClickListener {
EditText ettitle, etdescription, etdate; // et is edit text
String title;
String description;
String date;
InputStream is = null;
String result = null;
String line = null;
int code;
JSONArray json_data;
String custtitle[]; // cus means custom
String custdescrption[];
String custdate[];
// ListView Declaration
ListView lvcust;
Context c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news__updates);
lvcust = (ListView) findViewById(R.id.mylistview);
MyAsyncSearch asr = new MyAsyncSearch();
asr.execute();
}
// Insert AsyncTask
class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// insert();
return null;
}
}
// Search Async Task for Search query
class MyAsyncSearch extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
search();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// Fill listview with records through MyAdapter
MyAdapter aa = new MyAdapter(c, custtitle, custdescrption, custdate);
lvcust.setAdapter(aa);
}
}
// Search method for extracting records
public void search() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://10.0.0.1/FYP/admin/search_android.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception e) {
Log.e("Fail 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
result = line;
json_data = new JSONArray(result);
}
custtitle = new String[json_data.length()];
custdate = new String[json_data.length()];
custdescrption = new String[json_data.length()];
// Processing the result row by row and storing it in arrays called
// custnamelist and custphonelist
for (int i = 0; i < json_data.length(); i++) {
JSONObject jsonobject = json_data.getJSONObject(i);
title = jsonobject.getString("news_title");
date = jsonobject.getString("news_date");
description = jsonobject.getString("news_desc");
custtitle[i] = jsonobject.getString("news_title"); // (news_title
// database
// field
// name
custdate[i] = jsonobject.getString("news_date");
custdescrption[i] = jsonobject.getString("news_desc");
}
is.close();
} catch (Exception e) {
Log.e("Fail 2", e.toString());
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
我的logcat是
11-07 09:59:34.296: E/AndroidRuntime(1263): java.lang.NullPointerException: storage == null
11-07 09:59:34.296: E/AndroidRuntime(1263): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
11-07 09:59:34.296: E/AndroidRuntime(1263): at java.util.Arrays.asList(Arrays.java:154)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:141)
11-07 09:59:34.296: E/AndroidRuntime(1263): at com.example.bcs_final_project.MyAdapter.<init>(MyAdapter.java:17)
11-07 09:59:34.296: E/AndroidRuntime(1263): at com.example.bcs_final_project.News_Updates$MyAsyncSearch.onPostExecute(News_Updates.java:89)
11-07 09:59:34.296: E/AndroidRuntime(1263): at com.example.bcs_final_project.News_Updates$MyAsyncSearch.onPostExecute(News_Updates.java:1)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.os.AsyncTask.finish(AsyncTask.java:631)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.os.Looper.loop(Looper.java:137)
11-07 09:59:34.296: E/AndroidRuntime(1263): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 09:59:34.296: E/AndroidRuntime(1263): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 09:59:34.296: E/AndroidRuntime(1263): at java.lang.reflect.Method.invoke(Method.java:525)
11-07 09:59:34.296: E/AndroidRuntime(1263): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 09:59:34.296: E/AndroidRuntime(1263): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
适配器代码 公共类MyAdapter扩展ArrayAdapter { 字符串标题[],日期[],描述[]; 活动c;
@SuppressWarnings("unchecked")
public MyAdapter(Context context, String ti[], String dt[], String des[]) {
super(context, R.layout.custom_rows, R.id.title, ti);
c = (Activity) context;
title = ti;
date = dt;
description = des;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
super.getView(position, convertView, parent);
LayoutInflater l = c.getLayoutInflater();
View v = l.inflate(R.layout.custom_rows, null);
TextView tv1 = (TextView) v.findViewById(R.id.news_title);
TextView tv2 = (TextView) v.findViewById(R.id.news_description);
TextView tv3 = (TextView) v.findViewById(R.id.news_date);
tv1.setText(title[position]);
tv2.setText(description[position]);
tv1.setText(date[position]);
return v;
}
}
答案 0 :(得分:0)
在我看来,你永远不会实例化你的'c'变量,导致NPE。