1

我在 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 失败。

4

5 回答 5

1

json is null所以 NPE.You 无法从 JsonParser 中获取 JsonObject。

biodata = json.getJSONArray("biodata");//here json is null.

并检查您的json = jParser.AmbilJson(link_url);

ListView lv = getListView();并且在 setAdapter 到列表视图之后没有使用。

于 2012-06-21T12:26:17.577 回答
1

好吧,我认为你应该学会正确地阅读日志。从 logcat 告诉您的内容中检测问题更为重要。因此,您可以这样做:

首先,在 IDE 上启用行号。其次,看一下logcat中是如何描述问题的:

     java.lang.RuntimeException: Unable to start activity componentInfo{App.Biodata/App.Biodata.viewdata}: java.lang.NullPointerException   

所以在你的 viewdata 类中抛出了一个空指针异常。发生这种情况是因为您尝试从空对象访问方法。

三、继续查找问题原因:

   Caused by: java.lang.NullPointerException   E/AndroidRuntime(404):  at App.Biodata.viewdata.onCreate(viewdata.java:42).    

所以问题出在第 42 行的 viewdata.java 中。

于 2012-06-21T12:29:55.017 回答
0

我认为下面的行有错误。

ListView lv = getListView();

所以请在下面使用。

ListView lv = (ListView) findViewById(R.id.list);

lv = getListView();
于 2012-06-21T12:28:06.080 回答
0

您将活动扩展为列表活动,并且您尝试访问该制造商的 XML 布局。所以删除你的 setContentView(R.layout.viewdata);

谢谢

于 2012-06-21T12:28:51.727 回答
0

你这里有错误

 biodata = json.getJSONArray("biodata");

你必须把条件

if(json!=null)
{
    biodata = json.getJSONArray("biodata");
}

jParser.AmbilJson(link_url);一定失败了

于 2012-06-21T12:30:22.967 回答