2014-12-01 30 views
2

设置适配器时出现此错误。在我看来,我已经有一个ID为lv_cine的ListView。设置适配器时出现“空对象”错误

编辑1:添加了错误的代码,但它们与此相同。

编辑2:好吧,我设法让过去的第一个错误,但现在我得到一个类似:

12-01 20:43:44.445 7617-7617/com.example.maria.maria E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.maria.maria, PID: 7617 
    java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
      at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 
      at android.widget.ListView.setAdapter(ListView.java:487) 
      at com.example.maria.maria.Cinema$GetContacts.onPostExecute(Cinema.java:146) 
      at com.example.maria.maria.Cinema$GetContacts.onPostExecute(Cinema.java:67) 
      at android.os.AsyncTask.finish(AsyncTask.java:632) 
      at android.os.AsyncTask.access$600(AsyncTask.java:177) 
      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      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:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

这里是我的活动(Cinema.java):

public class Cinema extends Activity { 

private ProgressDialog pDialog; 
ListView listView; 
private static String url = "http://viniciuscoelho.com/android/dados"; 

// JSON Node nomes 
private static final String TAG_CATEGORIA = "cinema"; 
private static final String TAG_NOME = "nome"; 
private static final String TAG_DATA = "data"; 
private static final String TAG_ENDERECO = "endereco"; 
private static final String TAG_LINK = "link"; 
private static final String TAG_DESC = "desc"; 

// contacts JSONArray 
JSONArray dados = null; 

// Hashmap for ListView 
ArrayList<HashMap<String, String>> listaDados; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_cinema); 

    listView = (ListView) findViewById(R.id.lv_cine); 

    new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Cinema.this); 
     pDialog.setMessage("Carregando..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       dados = jsonObj.getJSONArray(TAG_CATEGORIA); 

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

        String nome = c.getString(TAG_NOME); 
        String data = c.getString(TAG_DATA); 
        String endereco = c.getString(TAG_ENDERECO); 
        String link = c.getString(TAG_LINK); 
        String desc = c.getString(TAG_DESC); 


        // tmp hashmap for single contact 
        HashMap<String, String> linha = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        linha.put(TAG_NOME, nome); 
        linha.put(TAG_DATA, data); 
        linha.put(TAG_ENDERECO, endereco); 
        linha.put(TAG_LINK, link); 
        linha.put(TAG_DESC, desc); 

        // adding contact to contact list 
        listaDados.add(linha); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       Cinema.this, listaDados, 
       R.layout.linha, new String[] { TAG_NOME, TAG_DATA, 
       TAG_ENDERECO, TAG_LINK, TAG_DESC }, new int[] { R.id.tvNome, 
       R.id.tvData, R.id.tvEndereco, R.id.tvLink, R.id.tvDescricao }); 

     //relaciona os dados ao próprio listview 
     listView.setAdapter(adapter); 
    } 

} 

} 

和VIEW(activity_cinema.xml):

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:padding="@dimen/activity_horizontal_margin" 
    android:scrollbars="none" 
    android:background="@color/pome"> 

    <ListView 
     android:id="@+id/lv_cine" 
     android:background="@color/pome" 
     android:layout_width="fill_parent" 
     android:scrollbars="none" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logo_cine" 
     android:clickable="false" 
     style="@style/Listas"> 

    </ListView> 

</RelativeLayout> 

linha.xml:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:id="@+id/linha" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:paddingTop="20dp" 
     android:id="@+id/tvNome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="22sp" 
     android:textColor="@color/White" 
     android:textStyle="bold" 
     android:text="Nome"/> 

    <TextView 
     android:id="@+id/tvData" 
     android:textStyle="bold" 
     android:paddingBottom="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_below="@+id/tvNome" 
     android:textSize="16sp" 
     android:text="Data" /> 

    <TextView 
     android:id="@+id/tvEndereco" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_below="@+id/tvData" 
     android:text="Endereco" 
     android:textColorLink="@color/White" 
     android:textSize="14sp" 
     android:autoLink="all" 
     /> 

    <TextView 
     android:id="@+id/tvLink" 
     android:paddingBottom="5dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_below="@+id/tvEndereco" 
     android:text="Link" 
     android:textColorLink="@color/LightYellow" 
     android:textSize="14sp" 
     android:autoLink="all" 
     /> 

    <TextView 
     android:id="@+id/tvDescricao" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_below="@+id/tvLink" 
     android:textSize="16sp" 
     android:text="Descricao" 
     android:paddingBottom="20dp" 
     /> 

</RelativeLayout> 
+1

你可以发布该公司吗?你的活动的XML布局? – enrique7mc 2014-12-01 22:03:04

+0

在问题中添加。 – 2014-12-01 22:13:32

回答

3

有一件事我可以看到到目前为止:

你没有初始化lista带有值的Dados,可以在此处完成:

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(Cinema.this); 
    pDialog.setMessage("Carregando..."); 
    pDialog.setCancelable(false); 
    pDialog.show(); 

    listaDados = new ArrayList<HashMap<String, String>>(); 
} 
1

一定要具备的要素:R.id.tvNome,R.id.tvData, R.id.tvEndereco, R.id.tvLink, R.id.tvDescricao

布局内:R.layout.linha

+0

我有一个linha.xml作为ListView中行的xml。我加载正确吗? – 2014-12-01 22:31:55

+0

再次看到我的答案并发布您的linha布局。 – Jorgesys 2014-12-01 22:37:49

+0

在问题中添加,我想我确实有这些元素。 – 2014-12-01 22:47:19

相关问题