-1

当我尝试执行此代码时出现此错误。当我尝试访问此活动时,它会强制关闭。我正在使用这个类来解析我的数据库中的数据以显示它。仅供参考,它只在我的数据库中显示关于个人数据的 1 行。

这是我的java文件

package com.bucheuw.gollege.portal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.bucheuw.gollege.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class DataPribadiActivity extends Activity {

    public String nim;
    JSONArray str_login = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.panel_datapribadi);

        Bundle b = getIntent().getExtras();
        nim = b.getString("par_kode");

        String link_url = "http://10.0.2.2/gollege/datapribadi.php?kode="+nim;
        //String link_url = "http://10.0.2.2/siakad-andro/detail-kal.php?kode=1";
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.AmbilJson(link_url);

        try {
            str_login = json.getJSONArray("dtpr");
            String datp = "";
            TextView isi = (TextView) findViewById(R.id.tdatapribadi);
            for(int i = 0; i < str_login.length(); i=0){
                JSONObject ar = str_login.getJSONObject(i);
                datp += ar.getString("nim")+"\n"+
                "Nama : "+ar.getString("nama")+"\n"+
                "Email : "+ar.getString("email")+"\n"+      
                "Kelas : "+ar.getString("kelas")+"\n"+
                "Prodi : "+ar.getString("prodi")+"\n"+
                "Alamat : "+ar.getString("alamat")+"\n"+
                "Telepon : "+ar.getString("telp")+"\n"+
                "Tempat Tanggal Lahir : "+ar.getString("ttl")+"\n\n";
            }
            isi.setText(datp);


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

这是logcat

01-27 15:48:24.803: E/AndroidRuntime(881): FATAL EXCEPTION: main
01-27 15:48:24.803: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to start           activity ComponentInfo{com.bucheuw.gollege/com.bucheuw.gollege.portal.DataPribadiActivity}:    java.lang.NullPointerException
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.os.Looper.loop(Looper.java:123) 
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-27 15:48:24.803: E/AndroidRuntime(881):  at java.lang.reflect.Method.invokeNative(Native Method)
01-27 15:48:24.803: E/AndroidRuntime(881):  at java.lang.reflect.Method.invoke(Method.java:507)
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 15:48:24.803: E/AndroidRuntime(881):  at dalvik.system.NativeStart.main(Native Method)
01-27 15:48:24.803: E/AndroidRuntime(881): Caused by: java.lang.NullPointerException
01-27 15:48:24.803: E/AndroidRuntime(881):  at com.bucheuw.gollege.portal.DataPribadiActivity.onCreate(DataPribadiActivity.java:34) 
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-27 15:48:24.803: E/AndroidRuntime(881):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-27 15:48:24.803: E/AndroidRuntime(881):  ... 11 more

谁能解决我的错误?先谢谢了。。

4

1 回答 1

0

我认为这行有问题

Bundle b = getIntent().getExtras();
 nim = b.getString("par_kode");

你不检查b价值null,所以如果你不向这个活动发送任何东西,你会得到NullPointerException

于 2013-01-27T11:16:01.130 回答