我想将 JSONArray 转换为 String [] 。
在我的代码中没有错误,但是当我在logcat中编译时错误引用了行:
detail[i] =
"NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------";
detail[]
为String[]
,info_p
为JSONArray
这是我的完整代码包含错误编译:
public class InfoPelanggaranActivity extends Activity{
private static final String TAG = InfoPelanggaranActivity.class.getCanonicalName();
public static JSONArray info_p;//delegated isi response
ListView _lv_detail;
Button _btn_gotoAdd;
String[] detail;
int arrayLength;
ArrayList<String> detailList;
ArrayAdapter<String> listAdapter;
public void onCreate(Bundle savedIntanceState){
super.onCreate(savedIntanceState);
Log.v(TAG, "Start onCreate InfoPelanggaranActivity");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_infopelanggaran);
_lv_detail = (ListView) findViewById(R.id.lv_detail);
_btn_gotoAdd = (Button) findViewById(R.id.btn_gotoAdd);
_btn_gotoAdd.setOnClickListener(new ClickKeAdd());
setDetails();
}
private String[] setDetails() {
// TODO Auto-generated method stub
arrayLength = info_p.length();
for(int i = 0; i<arrayLength; i++){
Log.v(TAG, "Start setDetails(), for Index: "+i);
JSONObject jsonMain = info_p.optJSONObject(i);
detail[i] = new String[]{
"NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------"};
}
Log.w(TAG, "detail[i]: "+detail.length);
detailList = new ArrayList<String>();
detailList.addAll(Arrays.asList(detail));
//
listAdapter = new ArrayAdapter<String>(this,R.layout.row_infopelanggaran,detailList);
_lv_detail.setAdapter(listAdapter);
return detail;
}
public class ClickKeAdd implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(InfoPelanggaranActivity.this,AddPelanggaranActivity.class);
startActivity(i);
}
}
}
这是 EXCEPTION :
09-30 18:41:58.226: E/AndroidRuntime(3711): FATAL EXCEPTION: main
09-30 18:41:58.226: E/AndroidRuntime(3711): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peti/com.peti.InfoPelanggaranActivity}: java.lang.NullPointerException
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.os.Looper.loop(Looper.java:137)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-30 18:41:58.226: E/AndroidRuntime(3711): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 18:41:58.226: E/AndroidRuntime(3711): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 18:41:58.226: E/AndroidRuntime(3711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-30 18:41:58.226: E/AndroidRuntime(3711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-30 18:41:58.226: E/AndroidRuntime(3711): at dalvik.system.NativeStart.main(Native Method)
09-30 18:41:58.226: E/AndroidRuntime(3711): Caused by: java.lang.NullPointerException
09-30 18:41:58.226: E/AndroidRuntime(3711): at com.peti.InfoPelanggaranActivity.setDetails(InfoPelanggaranActivity.java:54)
09-30 18:41:58.226: E/AndroidRuntime(3711): at com.peti.InfoPelanggaranActivity.onCreate(InfoPelanggaranActivity.java:40)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.Activity.performCreate(Activity.java:5104)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-30 18:41:58.226: E/AndroidRuntime(3711): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-30 18:41:58.226: E/AndroidRuntime(3711): ... 11 more
为什么会这样?
为什么我不能填写详细信息[]?
请有人帮帮我..
答案 0 :(得分:0)
好吧得到了......
detail[i] = new String[]{
"NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------"};
这是你的第54行
这里你使用大小为1的new运算符创建String Array,因为没有逗号分隔符 即
new String[]{
"NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------"};
并尝试将其分配到名为 detail [i] 的字符串中 这种类型的转换无法自动完成。
现在,解决方案是... arrayLength = info_p.length();
添加detail=new String[arrayLength]
和内部循环更改
detail[i] = new String[]{
"NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------"};
到
detail[i] = "NAMA SIM : "+jsonMain.optString("nama_pengendara").toString()+"\n"+
"IDENTITAS : "+jsonMain.optString("jenis_identitas").toString()+"\n"+
"NO.IDENTITAS : "+jsonMain.optString("no_identitas").toString()+"\n"+
"PELANGGARAN : "+jsonMain.optString("jenis_pelanggaran").toString()+"\n"+
"--------------------------------";