我的代码总是给出空指针异常
接收
时JSON数据没问题这是我的图书馆类
package com.jaggu.sitams;
public class library extends Activity {
ConnectionDetector cd;
Context c;
ProgressDialog pd;
String jsonResult;
TableRow row;
TableLayout table_layout;
Serverconnect2 sc;
JSONArray arr;
String url="http://sitams.url.ph/sitams/default.php",result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getActionBar();
if (ab != null) {
ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099CC")));
}
setContentView(R.layout.activity_library);
this.table_layout=(TableLayout) findViewById(R.id.tablelayout1);
dummyfun();
}
void dummyfun()
{
this.cd=new ConnectionDetector(getApplicationContext());
if (!this.cd.isConnectingToInternet())
{
Toast.makeText(getApplicationContext(),"Please check your internet connection",Toast.LENGTH_LONG).show();
}
else
{
Log.i("refresh","its running");
sc=new Serverconnect2(this,getApplicationContext());
sc.execute(url);
}
}
void getval(JSONObject json) {
//json values used here
JSONObject jc;
Log.i("hi","got here");
this.arr = json.optJSONArray("lib");
try {
for (int i = 0; i <arr.length(); i++) {
this.row = new TableRow(this);
this.row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
for (int j =0; j <=1; j++) {
jc = arr.getJSONObject(i);
TextView tv = new TextView(this);
//tv.setBackgroundResource(R.drawable.cell_shape);
tv.setPadding(5, 5, 5, 5);
tv.setText(jc.get("book").toString());
Log.i("book",jc.get("book").toString() );
this.row.addView(tv);
}
this.table_layout.addView(this.row);
}
}
catch (JSONException e) {
e.printStackTrace();
Log.i("hello", "error");
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
public class Serverconnect2 extends AsyncTask<String, Void, String> {
Activity a;
String jsonResult;
ProgressDialog pd;
Context c;
Exception e;
public Serverconnect2(Activity a, Context context)
{
this.a=a;
this.c=context;
}
@Override
protected void onPreExecute()
{
pd= ProgressDialog.show(a,null,"Loading...",true);
pd.setCancelable(false);
pd.show();
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
pd.dismiss();
if(e!=null){
Toast.makeText(a, ""+e,Toast.LENGTH_LONG).show();
}
try {
getval(new JSONObject(result));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
@Override
protected String doInBackground(String... arg0) {
try
{
HttpParams hp=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(hp, 20 * 1000);
HttpConnectionParams.setSoTimeout(hp,30*1000);
HttpClient httpclient=new DefaultHttpClient(hp);
HttpGet httppost=new HttpGet("http://sitams.16mb.com/sitams/lib.php");
HttpResponse response=httpclient.execute(httppost);
jsonResult=InputStreamToString(response.getEntity().getContent()).toString();
}
catch(ClientProtocolException e)
{
this.e=e;
Log.i("error",""+e);
return null;
}
catch(IOException e){
this.e=e;
Log.i("error",""+e);
return null;
}
catch(Exception e)
{
this.e=e;
Log.i("error", "" + e);
return null;
}
return jsonResult;
}
private StringBuilder InputStreamToString(InputStream is) {
String rLine="";
StringBuilder answer=new StringBuilder();
BufferedReader rd=new BufferedReader(new InputStreamReader(is));
try
{
while((rLine=rd.readLine())!=null){
answer.append(rLine);
}
}
catch(IOException e)
{
e.printStackTrace();
}
return answer;
}
}
}
这是我得到的错误。
02-14 00:28:47.421 6860-6860/com.jaggu.sitams W/System.err: java.lang.NullPointerException
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at com.jaggu.sitams.library.getval(library.java:108)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at com.jaggu.sitams.library$Serverconnect2.onPostExecute(library.java:152)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at com.jaggu.sitams.library$Serverconnect2.onPostExecute(library.java:125)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:631)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.os.Looper.loop(Looper.java:174)
02-14 00:28:47.441 6860-6860/com.jaggu.sitams W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4952)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
02-14 00:28:47.451 6860-6860/com.jaggu.sitams W/System.err: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我认为在初始化变量table_layout之前调用函数getVal()。请填写整个活动代码。