我在 logcat 中遇到了这个错误
06-21 11:58:27.275: E/AndroidRuntime(404): java.lang.RuntimeException: Unable to start activity ComponentInfo{App.Biodata/App.Biodata.viewdata}: java.lang.NullPointerException
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.os.Looper.loop(Looper.java:123)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-21 11:58:27.275: E/AndroidRuntime(404): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 11:58:27.275: E/AndroidRuntime(404): at java.lang.reflect.Method.invoke(Method.java:521)
06-21 11:58:27.275: E/AndroidRuntime(404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-21 11:58:27.275: E/AndroidRuntime(404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-21 11:58:27.275: E/AndroidRuntime(404): at dalvik.system.NativeStart.main(Native Method)
06-21 11:58:27.275: E/AndroidRuntime(404): Caused by: java.lang.NullPointerException
06-21 11:58:27.275: E/AndroidRuntime(404): at App.Biodata.viewdata.onCreate(viewdata.java:42)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 11:58:27.275: E/AndroidRuntime(404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-21 11:58:27.275: E/AndroidRuntime(404): ... 11 more
.java文件:
package App.Biodata;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class viewdata extends ListActivity {
private static String link_url = "http://10.0.2.2/biodatawebservice/view.php";
private static final String BIO_NAMA = "nama";
private static final String BIO_ALAMAT = "alamat";
private static final String BIO_TGLLAHIR = "tgl_lahir";
private static final String BIO_JK = "jk";
private static final String BIO_EMAIL = "email";
private static final String BIO_PHONE = "phone";
JSONArray biodata = null;
ArrayList<HashMap<String, String>> daftar_biodata = new ArrayList<HashMap<String, String>>();
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdata);
JSONParser jParser = new JSONParser();
JSONObject json = jParser.AmbilJson(link_url);
try {
biodata = json.getJSONArray("biodata");
for(int i = 0; i < biodata.length(); i++){
JSONObject bio = biodata.getJSONObject(i);
String nama = bio.getString(BIO_NAMA);
String alamat = bio.getString(BIO_ALAMAT);
String tgl_lahir = bio.getString(BIO_TGLLAHIR);
String jk = bio.getString(BIO_JK);
String email = bio.getString(BIO_EMAIL);
String phone = bio.getString(BIO_PHONE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(BIO_NAMA, nama);
map.put(BIO_ALAMAT, alamat);
map.put(BIO_TGLLAHIR, tgl_lahir);
map.put(BIO_JK, jk);
map.put(BIO_EMAIL, email);
map.put(BIO_PHONE, phone);
daftar_biodata.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
this.adapter_listview();
}
private void adapter_listview() {
// TODO Auto-generated method stub
ListAdapter adapter = new SimpleAdapter(this, daftar_biodata,
R.layout.list_data,
new String[] { BIO_NAMA, BIO_ALAMAT, BIO_TGLLAHIR, BIO_JK, BIO_EMAIL, BIO_PHONE}, new int[] {
R.id.txtNama, R.id.txtAlamat, R.id.txtTgllahir, R.id.txtJK, R.id.txtEmail,R.id.txtPhone});
setListAdapter(adapter);
ListView lv = getListView();
}
}
listactivity的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
每个项目的xml:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Nama"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtNama"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Alamat"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtAlamat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Tanggal Lahir"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtTgllahir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Jenis Kelamin"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtJK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Telepon"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Email"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFF" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#333"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
我找不到这个应用程序有什么问题,谁能告诉我为什么这个应用程序强制关闭?我已经在这里阅读了一些答案,告诉它xml布局文件有问题,但仍然找不到解决方案
提前致谢。
[解决了]
非常感谢stackoverflow.com成员帮助我,问题是这样的
biodata = json.getJSONArray("biodata");
为空,因为我的 php 失败。