我需要从mysql数据库中获取2列,所以我将它们传递到HashMap中,然后在alertdialog的listview中显示它们,但是我遇到了无法解决的错误
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
int sucess;
try {
List<NameValuePair> Ext = new ArrayList<NameValuePair>();
Ext.add(new BasicNameValuePair("NotificationID", NotificationID));
json3 = jsonParser2.makeHttpRequest(Get_Ext,"POST", Ext);
//json3 = jsonParser2.getJSONFromUrl(Get_Ext);
//getExt = json3.getString("FieldTitle");
//getExtVal=json3.getString("FieldValue");
Rec2 = new JSONArray(getExt);
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < Rec2.length(); i++){
JSONObject c = Rec2.getJSONObject(i);
map = new HashMap<String, String>();
map.put("FieldTitle", c.getString("FieldTitle"));
map.put("FieldValue", c.getString("FieldValue"));
MyArrList.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return null ;
}
这是我的onpostexecute:
@Override
protected void onPostExecute(String st){
Toast.makeText(getApplicationContext(), "done" , Toast.LENGTH_LONG).show();
SimpleAdapter ExtAdpt = new SimpleAdapter(getApplicationContext(), MyArrList, R.layout.extended_message, new String[]{"FieldTitle","FieldValue"}, new int[]{R.id.ExtTitle, R.id.ExtMessage});
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(Notifications.this);
LayoutInflater inflat = getLayoutInflater();
View turnview = (View) inflat.inflate(R.layout.extended_list, null);
alertDialogBuilder2.setView(turnview);
alertDialogBuilder2.setTitle("Call for Action ...");
Extlist = (ListView) turnview.findViewById(R.id.extendedlst);
Extlist.setAdapter(ExtAdpt);
alertDialogBuilder2.create();
alertDialogBuilder2.show();
}
我已经在浏览器上测试了我的php文件,效果很好
我的logcat
03-10 18:35:23.048: W/dalvikvm(1720): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-10 18:35:23.088: E/AndroidRuntime(1720): FATAL EXCEPTION: main
03-10 18:35:23.088: E/AndroidRuntime(1720): java.lang.NullPointerException
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.widget.ListView.setAdapter(ListView.java:462)
03-10 18:35:23.088: E/AndroidRuntime(1720): at com.example.notifier.Notifications$GetExtended.onPostExecute(Notifications.java:492)
03-10 18:35:23.088: E/AndroidRuntime(1720): at com.example.notifier.Notifications$GetExtended.onPostExecute(Notifications.java:1)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.os.Looper.loop(Looper.java:137)
03-10 18:35:23.088: E/AndroidRuntime(1720): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 18:35:23.088: E/AndroidRuntime(1720): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:35:23.088: E/AndroidRuntime(1720): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 18:35:23.088: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 18:35:23.088: E/AndroidRuntime(1720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 18:35:23.088: E/AndroidRuntime(1720): at dalvik.system.NativeStart.main(Native Method)
请帮我 :'(
整个代码:
package com.example.notifier;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.zip.Inflater;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.drive.metadata.internal.ParentDriveIdSet;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Notifications extends Activity {
Spinner tiFi , ty ;
ProgressDialog progs;
Button sh ;
SqlCommands sql;
String[] Fields , name , name2, name3;
SimpleCursorAdapter Adpt ;
ListView NotiLst , Extlist;
ImageView Hip, Ntype , arr ;
String getGrp_url = "http:///db_get_grp.php";
String Get_Ext = "http:///get_Extended.php";
String result , fld, bdy ,NotificationID , getExt , getExtVal;
JSONParser jsonParser = new JSONParser();
JSONParser jsonParser2 = new JSONParser();
JSONObject json = null ;
JSONObject json3 = null ;
private static final String TAG_SUCCESS = "success";
private static final String TAG_Rec = "Grp_Name";
private static final String TAG_RESULTS="result";
ArrayList<HashMap<String, String>> MyArrList;
JSONArray Rec = null;
JSONArray Rec2 = null;
JSONArray Rec3 = null;
final List<String> list1 = new ArrayList<String>();
final List<String> list2 = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tiFi = (Spinner) findViewById(R.id.timFilt);
ty = (Spinner) findViewById(R.id.typFilt);
NotiLst = (ListView) findViewById(R.id.msgListView);
Hip =(ImageView) findViewById(R.id.hi);
Ntype =(ImageView) findViewById(R.id.Prio);
sh =(Button) findViewById(R.id.dbadd);
Extlist = (ListView) findViewById(R.id.extendedlst);
new GetGroup().execute();
sh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* sql = new SqlCommands(getApplicationContext());
Boolean x = sql.AddEntry(2,"aaaa","fggdfg",2,3,3,"2016-03-07");
if (x==true){
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_LONG).show();
}
CusListView();*/
Toast.makeText(getApplicationContext(), NotificationID , Toast.LENGTH_LONG).show();
}
});
/* tiFi.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2) {
case 0:
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
*/
/*
arr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c = sql.GetData();
}
});
*/
NotiLst.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// TODO Auto-generated method stub
sql = new SqlCommands(getApplicationContext());
Cursor cr = (Cursor) NotiLst.getItemAtPosition(position);
//Cursor cr = sql.gettype((int) id);
if (cr.moveToFirst())
{
fld = cr.getString(cr.getColumnIndex("Types"));}
switch (fld) {
case "1":
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Notifications.this);
alertDialogBuilder.setTitle("Message");
if (cr.moveToFirst())
{
bdy = cr.getString(cr.getColumnIndex("Body"));}
alertDialogBuilder.setMessage(bdy);
alertDialogBuilder.create();
alertDialogBuilder.show();
break;
case "2":
if (cr.moveToFirst())
{
NotificationID = cr.getString(cr.getColumnIndex("NotificationID"));
}
new GetExtended().execute();
break;
}
}});
String[] items = new String[]{"Ever", "Today", "Yseterday", "Last Week" , "Last Month" };
ArrayAdapter<String> timAdpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
tiFi.setAdapter(timAdpt);
tiFi.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressLint("NewApi")
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
sql = new SqlCommands(getApplicationContext());
String[] mFields = new String[]{SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] tview = new int []{R.id.sbj,R.id.bdy,R.id.DT};
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
switch (position) {
case 1:
cal.add(Calendar.DATE, 0);
String d = dateFormat.format(cal.getTime());
Cursor c = sql.getDate(d) ;
SimpleCursorAdapter Dadpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , mFields, tview ,0 );
Dadpt.notifyDataSetChanged();
NotiLst.setAdapter(Dadpt);
break;
case 2:
cal.add(Calendar.DATE, -1);
String d1 = dateFormat.format(cal.getTime());
Cursor c1 = sql.getDate(d1) ;
SimpleCursorAdapter Dadpt1 = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c1 , mFields, tview ,0 );
Dadpt1.notifyDataSetChanged();
NotiLst.setAdapter(Dadpt1);
break;
case 3:
cal.add(Calendar.DATE, 0);
String d2 = dateFormat.format(cal.getTime());
Cursor c2 = sql.getDateweek(d2) ;
SimpleCursorAdapter Dadpt2 = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c2 , mFields, tview ,0 );
Dadpt2.notifyDataSetChanged();
NotiLst.setAdapter(Dadpt2);
break;
case 4:
cal.add(Calendar.DATE, -30);
String d3 = dateFormat.format(cal.getTime());
Cursor c3 = sql.getDate(d3) ;
SimpleCursorAdapter Dadpt3 = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c3 , mFields, tview ,0 );
Dadpt3.notifyDataSetChanged();
NotiLst.setAdapter(Dadpt3);
break;
default:
CusListView();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void spin(){
ArrayAdapter<String> grpAdpt = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, name);
ty.setAdapter(grpAdpt);
ty.setOnItemSelectedListener(new OnItemSelectedListener() {
@SuppressLint("NewApi")
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(parent.getContext(), "The planet is " +parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
switch (position) {
case 0:
CusListView();
break;
default:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.sbj,R.id.bdy,R.id.DT};
//int P = (int)parent.getItemAtPosition(position);
Cursor c = sql.getSpecificData(position) ;
SimpleCursorAdapter Adpt2 = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
Adpt2.notifyDataSetChanged();
NotiLst.setAdapter(Adpt2);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}}); }
@SuppressLint("NewApi")
private void CusListView(){
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
}
private void IsHigh(){
sql = new SqlCommands(getApplicationContext());
Cursor c = sql.GetData();
if (c.moveToFirst())
{
do {
int IShig = c.getColumnIndex("Priopity");
if(IShig!=1){
Hip.setVisibility(View.INVISIBLE);
}
else {
Hip.setVisibility(View.VISIBLE);
}
} while (c.moveToNext());
}
sql.close();
}
public void showSpinner(){
try {
JSONObject json;
Rec = new JSONArray(result);
name = new String[Rec.length()];
for(int i=0;i<Rec.length();i++){
json = Rec.getJSONObject(i);
name[i] = json.getString("Grp_Name");
}
for (int i=0; i<name.length; i++){
list1.add(name[i]);
}
spin();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class GetGroup extends AsyncTask<String, String, String >{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
json = jsonParser.getJSONFromUrl(getGrp_url);
Log.d("Login attempt", json.toString());
result = json.getString("Grp_Name");
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result){
showSpinner();
}
}
class GetExtended extends AsyncTask<String, String, String>{
@SuppressLint("NewApi")
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
int sucess;
try {
List<NameValuePair> Ext = new ArrayList<NameValuePair>();
Ext.add(new BasicNameValuePair("NotificationID", NotificationID));
json3 = jsonParser2.makeHttpRequest(Get_Ext,"POST", Ext);
//json3 = jsonParser2.getJSONFromUrl(Get_Ext);
//getExt = json3.getString("FieldTitle");
//getExtVal=json3.getString("FieldValue");
Rec2 = new JSONArray();
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < Rec2.length(); i++){
JSONObject c = Rec2.getJSONObject(i);
map = new HashMap<String, String>();
map.put("FieldTitle", c.getString("FieldTitle"));
map.put("FieldValue", c.getString("FieldValue"));
MyArrList.add(map);
}
} catch (Exception e) {
// TODO: handle exception
}
return null ;
}
@Override
protected void onPostExecute(String st){
Toast.makeText(getApplicationContext(), "done" , Toast.LENGTH_LONG).show();
SimpleAdapter ExtAdpt = new SimpleAdapter(getApplicationContext(), MyArrList, R.layout.extended_message, new String[]{"FieldTitle","FieldValue"}, new int[]{R.id.ExtTitle, R.id.ExtMessage});
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(Notifications.this);
LayoutInflater inflat = getLayoutInflater();
View turnview = (View) inflat.inflate(R.layout.extended_list, null);
alertDialogBuilder2.setView(turnview);
alertDialogBuilder2.setTitle("Call for Action ...");
Extlist = (ListView) turnview.findViewById(R.id.extendedlst);
Extlist.setAdapter(ExtAdpt);
alertDialogBuilder2.create();
alertDialogBuilder2.show();
}
}
}
最佳答案
MyArrList似乎是doInBackground方法的本地对象,您都不是从doInBackground方法返回的。或者,您尝试在onPostExecute中访问该对象,该对象为null或不退出。