2015-06-19 57 views
-3

程序/ APK需要列出所有的標題,但是當我運行它APK關閉請幫助我,我不知道到底發生了什麼如何列出數據庫列表視圖上的所有事件?

package com.eu.agendamarinhagrande; 

import android.annotation.TargetApi; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

import com.eu.agendamarinhagrande.JSONParser; 
import com.eu.agendamarinhagrande.R; 

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

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


public class MainActivity extends ActionBarActivity { 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // Creating JSON Parser object 
    JSONParser jParser = new JSONParser(); 

    ArrayList<HashMap<String, String>> empresaList; 


    // url to get all products list 
    private static String url_all_empresas = "http://www.grifin.pt/projectoamg/Conexao.php"; 

    // JSON Node names 


    private static final String TAG_TITULO = "Titulo"; 


    // products JSONArray 
    String resultado = null; 

    ListView lista; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Hashmap para el ListView 
     empresaList = new ArrayList<HashMap<String, String>>(); 
LoadAllProducts loadAllProducts = new LoadAllProducts(); 
     // Cargar los productos en el Background Thread 

     lista = (ListView) findViewById(R.id.agenda); 
loadAllProducts.execute(String.valueOf(lista)); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 

    }//fin onCreate 


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

     /** 
     * Antes de empezar el background thread Show Progress Dialog 
     */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("A carregar eventos. Por favor espere..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * obteniendo todos los productos 
     */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List params = new ArrayList(); 
      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(url_all_empresas, "GET", params); 
StringBuilder sb = new StringBuilder(); 
      // Check your log cat for JSON reponse 
      Log.d("All Products: ", url_all_empresas.toString()); 
      resultado = sb.toString(); 
      try { 
       // Checking for SUCCESS TAG 

       // products found 
       // Getting Array of Products 

        JSONArray arrayJson = new JSONArray(resultado); 
       for (int i = 0; i<arrayJson.length();i++){ 



       // Storing each json item in variable 
       JSONObject c = arrayJson.getJSONObject(i); 
       String Titulo = c.getString(TAG_TITULO); 


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

       // adding each child node to HashMap key => value 
       map.put(TAG_TITULO, Titulo); 


       empresaList.add(map); 
      } 

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

     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        ListAdapter adapter = new SimpleAdapter(
          MainActivity.this, 
          empresaList, 
          R.layout.single_post, 
          new String[]{ 
            TAG_TITULO 

          }, 
          new int[]{ 
            R.id.single_post_tv_id 

          }); 
        // updating listview 
        //setListAdapter(adapter); 
        lista.setAdapter(adapter); 
       } 
      }); 

     } 
    } 
} 
+2

看看你的logcat看什麼happend。如果你發現一個stracktrace請添加它。 – Jens

+1

發佈您的logcattttttttttttttttttttttttttttttttttttt。 –

+1

你需要修復你的鍵盤,你的「S」,「a」和「d」鍵似乎是失靈... – 2Dee

回答

0

入住這條線在你onCreate()方法:

loadAllProducts.execute(String.valueOf(lista)); 

你無法獲得ListView的字符串值!如果你想在ListView一個TextView的價值,你可以一個OnItemClick事件中選中的列表項這樣添加到您的ListView和得到的TextView的價值:

//a variable, you could use from all the methods, that will have as a value the value of the selected TextView 
private String selectedValue; 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    View view = (View)v.getParent(); 
    TextView textYouNeed = (TextView) view.findViewById(R.id.textViewId); 
    selectedValue = textYouNeed.getText(); 
} 

,然後使用這個變量在該行我告訴上面:

loadAllProducts.execute(selectedValue); 
+0

我真的很新的Android上,你可以做它在我的代碼?我嘗試它,並不工作我交易loadAllProducts.execute(String.valueOf(lista)); 爲您的代碼 –

+0

稍等片刻,我會送你一個簡單的代碼 –

+0

嘗試使用此代碼http://pastebin.com/M0EVrRkD,如果你得到任何錯誤(因爲我沒有把它給你之前測試),只是告訴我 –

0
06-19 11:07:34.233 13804-13804/? I/art﹕ Not late-enabling -Xcheck:jni (already on) 
06-19 11:07:34.380 13804-13818/com.eu.agendamarinhagrande I/art﹕ Background sticky concurrent mark sweep GC freed 1879(132KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 538KB/605KB, paused 49.704ms total 65.759ms 
06-19 11:07:34.380 13804-13804/com.eu.agendamarinhagrande D/AndroidRuntime﹕ Shutting down VM 
06-19 11:07:34.380 13804-13804/com.eu.agendamarinhagrande E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.eu.agendamarinhagrande, PID: 13804 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eu.agendamarinhagrande/com.eu.agendamarinhagrande.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      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) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference 
      at com.eu.agendamarinhagrande.MainActivity.onCreate(MainActivity.java:75) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5257) 
            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) 
+0

據說之後,該列表視圖是空的,所以請你能調試和行後檢查'LISTA =(ListView控件)findViewById(R.id.agenda) ;'lista'有什麼價值? –

+0

如何調試?取值列表?在文本視圖? –

+0

沒有,使用Android工作室(或者Eclipse)https://developer.android.com/tools/debugging/debugging-studio.html –

相關問題