我试图从服务器接受一些使用json的值并将其显示在自定义列表中..我面临的问题是接受服务器的值并在将值添加到数组列表时解码值我得到了这个错误...
FATAL EXCEPTION: main
Process: com.quinoid.pushnotification, PID: 1700
java.lang.NullPointerException
at com.quinoid.patient.MainActivity$JSONParse.onPostExecute(MainActivity.java:207)
at com.quinoid.patient.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我的json课程
private class JSONParse extends AsyncTask<String, String, JSONObject> {
DocGetSet DGS = new DocGetSet();
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected JSONObject doInBackground(String... args) {
JSONData jParser = new JSONData();
String url = "http://projects.quin.com/Chai/Connect/json/SearchingRange.php?range=5&devicetoken=swghfghjgh";
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
dialog.cancel();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_TITLE);
for (int i = 0; i < android.length(); i++) {
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
DGS.setPhoto(c.getString(TAG_IMAGE)); getting values here
DGS.setName(c.getString(TAG_NAME)); getting values here
DGS.setId(c.getString(TAG_ID)); getting values here
DGS.setDistance(c.getString(TAG_DISTANCE)); getting values here
//new DownloadTask().execute(DGS.getPhoto());
mylist.add(DGS);
-----------getting error here--------------------
list.setAdapter(new ApplistAdapter(getApplicationContext(),
mylist));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
那有什么不对?我不明白......
MainActivity.java
public class MainActivity extends ListActivity {
private static final String TAG_TITLE = "SearchingDoctors";
private static final String TAG_NAME = "Doctorsname";
private static final String TAG_SPECIALIZATION = "Specialization";
private static final String TAG_IMAGE = "ProfilePicture";
private static final String TAG_ID = "DoctorsId";
private static final String TAG_DISTANCE = "distance";
Spinner spin;
List<String> Speciality;
ArrayAdapter<String> myAdapter1;
String DeviceToken,speciality;
int progress = 0;
ArrayList<DocGetSet> mylist = new ArrayList<DocGetSet>();
ListView list;
JSONParser json = new JSONParser();
JSONArray android = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent myintent = getIntent();
DeviceToken = myintent.getStringExtra("DeviceToken");
//Connection detector
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
else
{
new JSONParse().execute();
}
main();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String name = ((TextView) v.findViewById(R.id.tvDoctorName)).getText().toString();
Toast.makeText(this, "You have selected : " + " " +name , Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(MainActivity.this, SelectedDoctor.class);
myIntent.putExtra("id", id);
startActivity(myIntent);
}
private void main() {
// TODO Auto-generated method stub
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1);
spin = (Spinner)findViewById(R.id.spinner1);
//SpinnerValues();
final TextView textView = (TextView)findViewById(R.id.textView2);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
textView.setText(""+progress+" Km");
speciality = spin.getSelectedItem().toString();
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(MainActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
else
{
new JSONParse().execute();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
// TODO Auto-generated method stub
progress = progresValue;
textView.setText(""+progress+" Km");
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
DocGetSet DGS = new DocGetSet();
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected JSONObject doInBackground(String... args) {
JSONData jParser = new JSONData();
String url = "http://projects.quin.com/Chai/Connect/json/SearchingRange.php?range=5&devicetoken=swghfghjgh";
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
dialog.cancel();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_TITLE);
for (int i = 0; i < android.length(); i++) {
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
DGS.setPhoto(c.getString(TAG_IMAGE));
DGS.setName(c.getString(TAG_NAME));
DGS.setId(c.getString(TAG_ID));
//DGS.setSpecialization(c.getString(TAG_SPECIALIZATION));
DGS.setDistance(c.getString(TAG_DISTANCE));
//new DownloadTask().execute(DGS.getPhoto());
mylist.add(DGS);
list.setAdapter(new ApplistAdapter(getApplicationContext(),
mylist));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
class DownloadTask extends AsyncTask<String, Void, Bitmap> {
private ProgressDialog dialog;
DocGetSet DGS = new DocGetSet();
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];
Bitmap photo;
JSONPhotoData PhotoData = new JSONPhotoData();
photo = PhotoData.getJSONPhotoFromUrl(url);
return photo;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
dialog.cancel();
// DGS.setImage(bitmap);
}
}
private class ApplistAdapter extends BaseAdapter {
Context mycon;
LayoutInflater inflater;
ArrayList<DocGetSet> data;
public ApplistAdapter(Context context, ArrayList<DocGetSet> mylist) {
// TODO Auto-generated constructor stub
mycon = context;
data = mylist;
inflater = (LayoutInflater) mycon
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
ViewHolder holder;
if (v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.row, null);
holder.DocName = (TextView) v.findViewById(R.id.tvDoctorName);
holder.Distance = (TextView) v.findViewById(R.id.tVDistance);
//holder.Specialization = (TextView) v.findViewById(R.id.tVSpecialization);
holder.id = (TextView) v.findViewById(R.id.tVDoctorID);
//holder.CimageView = (CircularImageView) v.findViewById(R.id.imageView1);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.DocName.setText(data.get(position).getName());
holder.Distance.setText(data.get(position).getDistance());
// holder.Specialization.setText(data.get(position).getSpecialization());
holder.id.setText(data.get(position).getId());
//holder.CimageView.setImageBitmap(data.get(position).getImage());
return v;
}
private class ViewHolder {
public TextView DocName;
public TextView Distance;
//public TextView Specialization;
public TextView id;
// public CircularImageView CimageView;
}
}
}
答案 0 :(得分:3)
NullPointerException
...
意味着您正在使用NULL的对象......
ListView 对象存在问题..
在使用之前初始化列表
list = (ListView)findViewById(R.id.yourlistviewname);
然后使用它......
你的代码的另一件事是......
在for循环之外设置列表适配器...
for(){....
}
list.setAdapter(new ApplistAdapter(getApplicationContext(),
mylist));
在for循环结束后输入此代码..
希望这会有所帮助
答案 1 :(得分:2)
让你初始化&#34; list&#34;对象
如果没有,请创建
list = findViewById(R.id .....);
答案 2 :(得分:1)
首先,您刚刚宣布您的ListView
未初始化。
ListView list;
所以你需要在main()
方法上初始化它。像,
list = (ListView)findViewById(R.id.yourlistviewname);
并在main()
执行之前调用AsyncTask
方法。
另一件事是只需将你的适配器类设置为for循环。
list.setAdapter(new ApplistAdapter(getApplicationContext(),
mylist));