我正在访问外部数据库(000webhost)以获取应用中的电子邮件地址并将其打印到ListView
。我从logcat中打印的服务器得到了正确的响应,但我得到了null pointer exception
。
这就是我的logcat的样子:
05-11 16:00:39.891 24149-24914/info.androidhive.loginandregistration E/Entity Response:﹕ {"email":[{"email":"adeel@gmail.com"},{"email":"yamini@gmail.com"},{"email":"mona@gmail.com"}]}{"tag":"DisplayFriends","error":true,"error_msg":"Unknown 'tag' value. It should be either 'login' or 'register'"}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
05-11 16:00:39.895 24149-24149/info.androidhive.loginandregistration D/AndroidRuntime﹕ Shutting down VM
05-11 16:00:39.898 24149-24149/info.androidhive.loginandregistration E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.loginandregistration, PID: 24149
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at info.androidhive.loginandregistration.FriendsList.setListAdapter(FriendsList.java:55)
at info.androidhive.loginandregistration.FriendsList$GetAllCustomerTask.onPostExecute(FriendsList.java:74)
at info.androidhive.loginandregistration.FriendsList$GetAllCustomerTask.onPostExecute(FriendsList.java:60)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
以下是我的java文件: ApiConnector.java
public class ApiConnector {
Global g=Global.getInstance();
String a=g.getLogInEmail();
String b="DisplayFriends";
public JSONArray GetAllCustomers()
{
// URL for getting all customers
// String url = "http://z.locationtest.comxa.com/index.php?tag=DisplayFriends&loggedInEmail="+a;
String url = "http://z.locationtest.comxa.com/index.php";
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
HttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpPost httpPost = new HttpPost(url);
try
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", b));
params.add(new BasicNameValuePair("loggedInEmail", a));
httpPost.setEntity(new UrlEncodedFormEntity(params));
// HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray=null;
JSONObject jsonObject =null;
if (httpEntity != null)
{
try
{
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response: ", entityResponse);
jsonObject = new JSONObject(entityResponse);
jsonArray = jsonObject.getJSONArray("email");
// jsonArray = new JSONArray(entityResponse);
/*for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject c = jsonArray.getJSONObject(i);
Log.d("TAG_LOCATIONS", jsonArray.toString(i));
}*/
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return jsonArray;
}
}
FriendList.java
public class FriendsList extends Activity {
private ListView listViewFriends;
private JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listViewFriends=(ListView)this.findViewById(R.id.listViewFriends);
new GetAllCustomerTask().execute(new ApiConnector());
}
void setListAdapter(JSONArray jsonArray)
{
//this.jsonArray=jsonArray;
this.listViewFriends.setAdapter(new GetAllFriendsListViewAdapter(jsonArray,this));
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].GetAllCustomers();
}
@Override
protected void onPostExecute(JSONArray jsonArray)
{
setListAdapter(jsonArray);
}
}
}
GetAllFriendsListViewAdapter.java
public class GetAllFriendsListViewAdapter extends BaseAdapter
{
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater= null;
public GetAllFriendsListViewAdapter(JSONArray jsonArray, Activity a)
{
this.activity=a;
this.dataArray=jsonArray;
inflater= (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return this.dataArray.length();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ListCell cell;
if(convertView==null)
{
convertView=inflater.inflate(R.layout.item_layout,null);
cell=new ListCell();
cell.email=(TextView)convertView.findViewById(R.id.textViewFriends);
convertView.setTag(cell);
}
else
{
cell=(ListCell)convertView.getTag();
}
try
{
JSONObject jsonObject = this.dataArray.getJSONObject(position);
cell.email.setText(" "+jsonObject.getString("email"));
}
catch(JSONException e)
{
e.printStackTrace();
}
return convertView;
}
private class ListCell
{
private TextView email;
}
}
我是Android新手。如果有人帮我在代码中找到错误,那就太好了。
答案 0 :(得分:0)
每当您的程序运行时,都会在运行时抛出NullPointerException 尝试使用null,就好像它是一个真正的引用。
因此,错误不在于从您托管它的任何地方获取数据。这是因为你试图使用一个null的变量
你已经完成了这个private JSONArray jsonArray;
,但你也需要这样做variable = new variable-type();
来分配内存。
请检查上面的部分,并做一些谷歌搜索。获取数据没有错误,当您尝试使用它时会发生这种情况。
您只是声明变量如下
private ListView listViewFriends;
private JSONArray jsonArray;
但这还不够。 您还需要在代码中定义它们。