2016-02-13 17 views
1

我的代碼總是給一個空指針異常附加錶行編程的Android

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

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) –

+0

查看錯誤。它告訴你:'at com.jaggu.sitams.library.getval(library.java:108)'看看你的IDE /編輯器 - 問題是在第108行。 –

+0

table_layout.addView(row,new TableLayout.LayoutParams ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); – jagapathi

回答

0

我認爲函數getVal()在變量table_layout初始化之前調用。請放置整個活動代碼。

+0

我編輯了我的代碼先生請檢查我​​需要回答我的項目工作很差 – jagapathi

+0

我跑了你的代碼,它的運行成功。請添加activity_library.xml文件 –

+0

我修正了它先生這是問題在XML – jagapathi