我在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”);
为null,因为我的php失败了。
答案 0 :(得分:1)
json is null
所以NPE.You无法从JsonParser获取JsonObject。
biodata = json.getJSONArray("biodata");//here json is null.
并检查您的json = jParser.AmbilJson(link_url);
并且在setAdapter之后没有使用ListView lv = getListView();
到listview。
答案 1 :(得分:1)
嗯,我认为你应该学会正确阅读日志。从logcat告诉您的内容中检测问题更为重要。所以你可以这样做:
首先,在IDE上启用行号。 其次,看一下logcat中如何描述问题:
java.lang.RuntimeException: Unable to start activity componentInfo{App.Biodata/App.Biodata.viewdata}: java.lang.NullPointerException
因此,在viewdata类中抛出了一个空指针异常。发生这种情况是因为您尝试从null对象访问方法。
第三,继续寻找问题的原因:
Caused by: java.lang.NullPointerException E/AndroidRuntime(404): at App.Biodata.viewdata.onCreate(viewdata.java:42).
所以问题出在你的viewdata.java第42行。
答案 2 :(得分:0)
我认为下面的行有错误。
ListView lv = getListView();
所以请在下面使用。
ListView lv = (ListView) findViewById(R.id.list);
lv = getListView();
答案 3 :(得分:0)
您将活动扩展为List活动,并且您也尝试访问制造商的XML布局。所以删除你的setContentView(R.layout.viewdata);
由于
答案 4 :(得分:0)
你这里有错误
biodata = json.getJSONArray("biodata");
你必须把条件
if(json!=null)
{
biodata = json.getJSONArray("biodata");
}
您的jParser.AmbilJson(link_url);
必须失败