반응형
어댑터를 설정할 때 "Null object" 오류가 발생함
어댑터를 설정하면 이 에러가 발생합니다.ID를 가진 ListView가 이미 있습니다.lv_cine
내가 보기엔
Edit 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);
}
}
}
표시(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>
내가 지금까지 볼 수 있는 한 가지는:
값을 사용하여 listaDados를 초기화하지 않았습니다. 여기서 수행할 수 있습니다.
@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>>();
}
다음 요소가 있는지 확인합니다.R.id.tvNome,R.id.tvData, R.id.tvEndereco, R.id.tvLink, R.id.tvDescricao
레이아웃 내부:R.layout.linha
언급URL : https://stackoverflow.com/questions/27237936/null-object-error-when-setting-a-adapter
반응형
'programing' 카테고리의 다른 글
의 무료 데이터베이스 관리 시스템을 선택했습니다.NET 웹 사이트 (0) | 2022.09.22 |
---|---|
커스텀 스크립트를 패키지에 추가하려면 어떻게 해야 하나요?jascript 파일을 실행하는 json 파일? (0) | 2022.09.22 |
파일을 C/C++ 소스 코드 배열로 변환하는 스크립트/도구 (0) | 2022.09.22 |
테이블 셀에서의 VueJ 이행 (0) | 2022.09.22 |
JavaScript를 사용하여 숫자의 소수점 부분 가져오기 (0) | 2022.09.22 |