gpt4 book ai didi

java - Android:无法检索日期

转载 作者:行者123 更新时间:2023-11-29 23:28:00 25 4
gpt4 key购买 nike

我是 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)

适配器代码 公共(public)类 MyAdapter 扩展 ArrayAdapter { 字符串标题[]、日期[]、描述[]; Activity 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;
}
}

最佳答案

在我看来,您从未实例化“c”变量,从而导致 NPE。

关于java - Android:无法检索日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26804040/

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