gpt4 book ai didi

php - eclipse 在 InBackground 方法中显示错误我不知道如何修复它

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

我有一个android应用程序,需要使用php在android和mysql数据库之间进行连接,该应用程序工作完美。只有显示用户操作不起作用,此方法在包含一些数据的列表中显示用户。

谁能帮我解决这个错误?

LogCat 错误

       04-14 21:37:37.378: E/AndroidRuntime(1854): Caused by: java.lang.NullPointerException
04-14 21:37:37.378: E/AndroidRuntime(1854): at com.devleb.loginDemo.UserListActivity$getUserList.doInBackground(UserListActivity.java:143)
04-14 21:37:37.378: E/AndroidRuntime(1854): at com.devleb.loginDemo.UserListActivity$getUserList.doInBackground(UserListActivity.java:1)
04-14 21:37:37.378: E/AndroidRuntime(1854): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-14 21:37:37.378: E/AndroidRuntime(1854): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-14 21:37:37.378: E/AndroidRuntime(1854): ... 4 more

用户.java

package com.devleb.loginDemo;

import java.util.HashMap;

//@SuppressWarnings("serial")
public class User extends HashMap<String, String> {

/**
*
*/
String name;
String date;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

}

自定义适配器.java

package com.devleb.loginDemo;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

private static ArrayList<User> usersList;

// private static ArrayList name, date;
LayoutInflater layoutInflater;
String[] userName;
Date[] createdDate;
Context context;

@SuppressWarnings("rawtypes")
public CustomAdapter(ArrayList<User> result, Context c) {
usersList = result;
context = c;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return usersList.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return usersList.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int position, View arg1, ViewGroup parent) {
// TODO Auto-generated method stub

layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = null;

row = layoutInflater.inflate(R.layout.list_item, parent, false);
TextView txtCreateDate = (TextView) row
.findViewById(R.id.txtCreateDate);

txtCreateDate.setText(usersList.get(position).getDate().toString());

TextView txtName = (TextView) row.findViewById(R.id.name);

txtName.setText(usersList.get(position).getName());
return row;
}

}

UserListActivity.java

package com.devleb.loginDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class UserListActivity extends ListActivity {

JSONParser jsonParser = new JSONParser();

// ArrayList<HashMap<String, String>> usersList;

// **** added from internt*************///
@SuppressWarnings("rawtypes")
ArrayList<User> arrayList_userList;
ListView lvUsers;
// **** added from internt*************///

private static String url_display_user = "http://10.0.3.2/android_connect/display_user.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

private static final String TAG_ID = "id";

private static final String TAG_USERS = "userlist";

private static final String TAG_USER = "user";
private static final String TAG_DATE = "date";

// private static final String TAG_NAME = "name";

// employees JSONArray
JSONArray users = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);

// usersList = new ArrayList<HashMap<String, String>>();

new getUserList().execute();

// getListView
lvUsers = (ListView) findViewById(android.R.id.list);

lvUsers.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {

// String id = ((TextView)
// view.findViewById(R.id.uid)).getText()
// .toString();

// Intent in = new Intent(getBaseContext(), StatusList.class);
// in.putExtra(TAG_ID, uid);

// startActivity(in);
}
});
}

class getUserList extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();

UserListActivity.this.setProgressBarIndeterminateVisibility(true);
}

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

// Building Parameters
List<NameValuePair> parametres = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(url_display_user,
"GET", parametres);

// Check your log cat for JSON reponse
Log.d("All Users: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
users = json.getJSONArray(TAG_USERS);

// looping through All Users
for (int i = 0; i < users.length(); i++) {
JSONObject c = users.getJSONObject(i);

// Storing each json item in variable
String id = c.getString(TAG_ID);
String user = c.getString(TAG_USER);
String cdate = c.getString(TAG_DATE).toString();

// creating new HashMap
User map = new User();

// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_USER, user);
map.put(TAG_DATE, cdate);

// adding HashList to ArrayList

// usersList.add(map);

arrayList_userList.add(map);
}
// ***********************************//
// From the internet

// arrayList_userList = new
// ArrayList<User>(usersList.value())

// **********************************//

return json.getString(TAG_MESSAGE);
} else {

return json.getString(TAG_MESSAGE);

}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String result) {
// dismiss the dialog after getting all products
if (result != null) {

UserListActivity.this
.setProgressBarIndeterminateVisibility(false);

/**
* Updating parsed JSON data into ListView
* */
/*
* ListAdapter adapter = new
* SimpleAdapter(UserListActivity.this, usersList,
* R.layout.list_item, new String[] { TAG_ID, TAG_USER }, new
* int[] { R.id.uid, R.id.name }); // updating listview
* setListAdapter(adapter);
*/
lvUsers.setAdapter(new CustomAdapter(arrayList_userList,
getBaseContext()));

Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG)
.show();

}
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_list, menu);
return true;
}

}

最佳答案

它会准确地告诉您错误所在。您遇到空指针异常。

来自你的日志猫:

04-14 21:37:37.378: E/AndroidRuntime(1854): Caused by: java.lang.NullPointerException
04-14 21:37:37.378: E/AndroidRuntime(1854): at com.devleb.loginDemo.UserListActivity$getUserList.doInBackground(UserListActivity.java:143)
04-14 21:37:37.378: E/AndroidRuntime(1854): at com.devleb.loginDemo.UserListActivity$getUserList.doInBackground(UserListActivity.java:1)
04-14 21:37:37.378: E/AndroidRuntime(1854): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-14 21:37:37.378: E/AndroidRuntime(1854): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-14 21:37:37.378: E/AndroidRuntime(1854): ... 4 more

在 UserListActivity 第 143 行中,您似乎正在添加到 null ArrayList。快速浏览一下该类,您似乎从未初始化 arrayList_userList

关于php - eclipse 在 InBackground 方法中显示错误我不知道如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23071005/

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