2014-04-14 89 views
0

我有Android應用程序,需要連接Android和MySQL數據庫使用PHP的應用程序工作完美。只有顯示用戶操作不起作用此方法將用戶顯示在具有某些數據的列表中。Android應用程序擴展BaseAdapter和使用Asyncktask的錯誤

任何人都可以幫助我解決這個錯誤?

添加的代碼的結果是

04-14 23:17:57.766: D/this from internet(2367): usersList.get(position).getDate() is null 

logcat的錯誤

04-14 21:59:22.170: E/AndroidRuntime(1951): FATAL EXCEPTION: main 
04-14 21:59:22.170: E/AndroidRuntime(1951): java.lang.NullPointerException 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at com.devleb.loginDemo.CustomAdapter.getView(CustomAdapter.java:60) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.AbsListView.obtainView(AbsListView.java:2177) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.ListView.makeAndAddView(ListView.java:1840) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.ListView.fillDown(ListView.java:675) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.ListView.fillFromTop(ListView.java:736) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.ListView.layoutChildren(ListView.java:1655) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.AbsListView.onLayout(AbsListView.java:2012) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.View.layout(View.java:14289) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewGroup.layout(ViewGroup.java:4562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.View.layout(View.java:14289) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewGroup.layout(ViewGroup.java:4562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.View.layout(View.java:14289) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewGroup.layout(ViewGroup.java:4562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.View.layout(View.java:14289) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewGroup.layout(ViewGroup.java:4562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.View.layout(View.java:14289) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewGroup.layout(ViewGroup.java:4562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.Choreographer.doFrame(Choreographer.java:532) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.os.Handler.handleCallback(Handler.java:730) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.os.Looper.loop(Looper.java:137) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at java.lang.reflect.Method.invoke(Method.java:525) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-14 21:59:22.170: E/AndroidRuntime(1951):  at dalvik.system.NativeStart.main(Native Method) 

list_item.xml
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background_1" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/uid" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="6dip" 
     android:paddingTop="20dip" 
     android:text="text view" 
     android:textColor="#FFFFFF" 
     android:textSize="17dip" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/txtCreateDate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="TextView" 
     android:textColor="#FFFFFF" /> 

</RelativeLayout> 

User.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; 
    } 

} 

CustomAdapter.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 = new ArrayList<User>(); 

    // 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()); 

     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; 
    } 

} 
+1

「CustomAdapter」的第60行是什麼? – Naddy

+0

這行代碼** txtCreateDate.setText(usersList.get(position).getDate()。toString()); ** – user3499032

+0

發佈您的R.layout.list_item以及。 – uDevel

回答

0

您沒有setDate(),所以getDate()爲null。

    // 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); 

我沒有看到任何使用HashMap的任何代碼。但是爲了測試,請在這裏留下。

添加 map.setName(user); map.setDate(cdate);

就是這樣。

+0

以及你告訴我把它放在用戶類中的標籤,所以我對它做了什麼? – user3499032

+0

保留所有內容並添加兩行,以便演示。 – uDevel

+0

你真的是一個格柵人現在我明白你的意思是拋出hashmap。謝謝。但我仍然有一個問題,如果你可以幫助我 – user3499032

0

將這個

Log.d(TAG, txtCreateDate == null ? "txtCreateDate is null" : usersList.get(position) == null ? "usersList.get(position) is null" : usersList.get(position).getDate() == null ? "usersList.get(position).getDate() is null" : "nothing is null"); 

之前

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

另外,不要你需要初始化usersList?

+0

日誌顯示返回是空的? – user3499032

+0

對不起,請更具體。 「usersList.get(position).getDate()爲null」? – uDevel

+0

getDate方法是來自User類的方法此方法必須返回來自mysql數據庫的值,所以是我的代碼錯誤? – user3499032

相關問題