2015-01-21 65 views
-1

我應該創建一個應用程序,在成功登錄後,會有一個爲用戶發送的請求的列表視圖。我的登錄工作正常。但是當我開始創建列表視圖時,我的問題就開始了。當我將listview添加到我的應用程序時,出現這些錯誤

我在做一個listview教程,我測試了它,工作正常,所以我將它合併到我的代碼中。但是,當我這樣做,我的應用程序開始崩潰

errors in my logcat enter image description here

這是我有ListView的活動代碼。

import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.io.OutputStreamWriter; 
    import java.net.URL; 
    import java.net.URLConnection; 
    import java.net.URLEncoder; 
    import java.util.ArrayList; 

    import org.json.JSONArray; 
    import org.json.JSONObject; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.SharedPreferences; 
    import android.content.SharedPreferences.Editor; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.ListView; 
    import android.widget.Toast; 

    public class AdminActivity extends Activity { 

     ArrayList<Actors> actorsList; 

     ActorAdapter adapter; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_admin); 
      Bundle extras = getIntent().getExtras(); 
      String usertype = extras.getString("UserType"); 
      String userid = extras.getString("User_ID"); 
      actorsList = new ArrayList<Actors>(); // EDITED I FORGOT THIS 
      //PART, IT FIXED THE LOG IN ISSUE BUT STILL THE LIST VIEW DOESNT 
      //DISPLAY AND AFTER DIRECTING ME TO THE NEXT PAGE, IT CRASHES AFTER AWHILE 
      new GetRequests(this).execute(userid,usertype); 
      ListView listview = (ListView)findViewById(R.id.list); 
      adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList); 

      listview.setAdapter(adapter); 
      Log.i("lv", "does it go here"); 
      listview.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
         long id) { 
        Log.i("lvclick", "does it go here"); 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();    
       } 
      }); 
      Toast.makeText(this.getBaseContext(), userid + usertype, 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.admin, menu); 
      return true; 
     } 

     public class GetRequests extends AsyncTask<String,Void,String>{ 

      private Context context; 
      public GetRequests(Context context) { 
       this.context = context; 
      } 

      protected void onPreExecute(){ 

      } 

       @Override 
       protected String doInBackground(String... arg0) { 
       try { 
       Log.i("fetch", "does it go here"); 
         String userid = (String)arg0[0]; 

         String usertype = (String)arg0[1]; 

         String link="http://192.168.1.37/mobile/get-requests.php"; 

         String data = URLEncoder.encode("userid", "UTF-8") + "=" + URLEncoder.encode(userid, "UTF-8"); 

         data += "&" + URLEncoder.encode("usertype", "UTF-8") + "=" + URLEncoder.encode(usertype, "UTF-8"); 

         URL url = new URL(link); 

         URLConnection conn = url.openConnection(); 

         conn.setDoOutput(true); 

         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

         wr.write(data); 

         wr.flush(); 

         BufferedReader reader = new BufferedReader (new InputStreamReader(conn.getInputStream())); 

         StringBuilder sb = new StringBuilder(); 

         String line = null; 

         // Read Server Response 

         while((line = reader.readLine()) != null) 
         { 
          sb.append(line); 
          break; 
         } 
         return sb.toString(); 
       } 

       catch(Exception e){ 

        return new String("Exception: " + e.getMessage()); 

       } 

       } 

      @Override 
       protected void onPostExecute(String result){ 

       if(result.length() == 0){ 
        Toast.makeText(this.context, "Invalid User Id or Password", Toast.LENGTH_LONG).show(); 
       } 
       else{ 

        try { 
         Log.i("got", "does it go here"); 
          JSONArray jArr = new JSONArray(result); 
          for(int i = 0; i < jArr.length(); i++){ 
           JSONObject jobj = jArr.getJSONObject(i); 
           Actors actor = new Actors(); 

           actor.setName(jobj.getString("Request_ID")); 
           actor.setDescription(jobj.getString("User_ID")); 
           actor.setDob(jobj.getString("Date_Needed")); 
           actor.setCountry(jobj.getString("fromTime_Needed")); 
           actor.setHeight(jobj.getString("fromTime_Needed")); 
           actor.setSpouse(jobj.getString("Subject")); 
           actor.setChildren(jobj.getString("Group")); 
           actor.setImage(jobj.getString("Prof_ID")); 

           actorsList.add(actor); 

          } 
          Toast.makeText(this.context, result, Toast.LENGTH_LONG).show(); 


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


       } 
       } 
      } 

     public void logout(View view){ 
       SharedPreferences sharedpreferences = getSharedPreferences 
       (MainActivity.MyPREFERENCES, Context.MODE_PRIVATE); 
       Editor editor = sharedpreferences.edit(); 
       editor.clear(); 
       editor.commit(); 
       Log.i("logout", "does it go here?"); 
       moveTaskToBack(true); 
       AdminActivity.this.finish(); 
      } 
     public void exit(View view){ 
      moveTaskToBack(true); 
      AdminActivity.this.finish(); 
     } 
    } 

沒有這個塊,它會先顯示我的登錄名,然後它指示我到我設置意圖的地方。

listview.setAdapter(adapter); 
      Log.i("lv", "does it go here"); 
      listview.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
         long id) { 
        Log.i("lvclick", "does it go here"); 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();    
       } 
      }); 

與上面的代碼塊,我的應用程序將在我打開它後立即崩潰。

*編輯:修正了登錄問題,但一段時間後仍然崩潰。什麼似乎是問題?

回答

0

actorsList變量永遠不會被初始化。

變化:ArrayList<Actors> actorsList; 到:ArrayList<Actors> actorsList = new ArrayList();

請記住,當你調用actorsList.add(actor);你必須通知有數據變化。

理想情況下,您可以選擇使用ArrayAdapter,因爲您有方便的方法來添加新值。您可以在本地初始化並覆蓋getView方法,或者您可以讓ActorAdapter擴展此類型

2

看起來您沒有初始化actorsList;,這會拋出NPE。所以,你的宣言變成

ArrayList<Actors> actorsList = new ArrayList<Actors>(); 

還有你的另一問題是,你正在努力執行你的AsyncTask後直接設置Adapter一個空列表,因爲你這樣做。由於它運行「異步」,它不會在調用之前完成對setAdaoter()

您需要

ListView listview = (ListView)findViewById(R.id.list); 
adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList); 

listview.setAdapter(adapter); 

移動這個代碼onPostExecute()所以一旦任務完成就可以加載Adapter

此外,請不要粘貼你的堆棧跟蹤的截圖......這是很難閱讀。您可以單擊保存按鈕並將其粘貼到代碼塊中的文章中,就像您使用代碼一樣。

+0

我試過了,它幫助但仍然不顯示列表視圖。 – ladyisfirst 2015-01-21 06:38:39

+0

它仍然崩潰或只是listview不顯示? – 2015-01-27 09:14:04

相關問題