2016-09-18 98 views
-3

我想通過適配器將JSON數據傳遞給listview。現在,我得到了在logcat的一個錯誤:嘗試調用虛擬方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'null對象引用致命錯誤

09-18 17:03:46.787 6429-6429/ir.homa E/AndroidRuntime: FATAL EXCEPTION: main 
                Process: ir.homa, PID: 6429 
                java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.homa/ir.homa.HotelDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                 at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                 at android.os.Looper.loop(Looper.java:135) 
                 at android.app.ActivityThread.main(ActivityThread.java:5221) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at java.lang.reflect.Method.invoke(Method.java:372) 
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
                 at ir.homa.HotelDetail.onCreate(HotelDetail.java:43) 
                 at android.app.Activity.performCreate(Activity.java:5937) 
                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  
                 at android.app.ActivityThread.access$800(ActivityThread.java:144)  
                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  
                 at android.os.Handler.dispatchMessage(Handler.java:102)  
                 at android.os.Looper.loop(Looper.java:135)  
                 at android.app.ActivityThread.main(ActivityThread.java:5221)  
                 at java.lang.reflect.Method.invoke(Native Method)  
                 at java.lang.reflect.Method.invoke(Method.java:372)  
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  

這是我的適配器:

package ir.homa; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

/** 
* Created by SMQ on 9/17/2016. 
*/ 
public class RoomTypeAdapter extends ArrayAdapter<RoomType> { 
    public RoomTypeAdapter(Context context, ArrayList<RoomType> roomType) { 
     super(context,0, roomType); 
    } 
    @Override 
    public View getView(int position , View ConvertView, ViewGroup parent){ 
     RoomType roomType = getItem(position); 
     if (ConvertView == null) { 

      ConvertView = LayoutInflater.from(getContext()).inflate(R.layout.item_type, parent, false); 
     } 
     TextView typeItem = (TextView) ConvertView.findViewById(R.id.itemRoomType); 
     typeItem.setText(roomType.RoomType); 
     return ConvertView; 
    } 



} 

這是我的類,它包含的變量:

package ir.homa; 

import org.json.JSONException; 
import org.json.JSONObject; 

/** 
* Created by SMQ on 9/17/2016. 
*/ 
public class RoomType { 
    String RoomType; 
    String id; 
    public void setRoomType(JSONObject object) { 
     try { 
      this.RoomType = object.getString("description"); 
      this.id = object.getString("id"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

,這是我的MainActivity:

package ir.homa; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 

/** 
* Created by SMQ on 9/4/2016. 
*/ 
public class HotelDetail extends Activity { 


    // json array response url 
    private String urlJsonArry = "http://homa.qadamgahi.ir/bandar_result.json"; 
    private static String TAG = HotelDetail.class.getSimpleName(); 


    ArrayList<RoomType> typeList; 
    RoomTypeAdapter adapter; 
    ListView listView; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     typeList = new ArrayList<>(); 
     adapter = new RoomTypeAdapter(this, typeList); 
     listView = (ListView) findViewById(R.id.listv); 
     listView.setAdapter(adapter); 



     makeJsonObjectRequest(); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     /* 
     ATTENTION: This was auto-generated to implement the App Indexing API. 
     See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
     GoogleApiClient client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    /** 
    * Method to make json object request where json response starts wtih { 
    * */ 
    private void makeJsonObjectRequest() { 



     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
       urlJsonArry, null, new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, response.toString()); 

       // Parsing json object response 
       // response will be a json object 

       RoomType o = new RoomType(); 

       try { 

        for (int i = 0 ; i < typeList.size() ; i++) 
        { 


         RoomType add = new RoomType(); 
         add.id = response.getString("id"); 
         add.RoomType = response.getString("description"); 
         typeList.add(add); 
        } 

        adapter.notifyDataSetChanged(); 


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


      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       // hide the progress dialog 

      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(jsonObjReq); 
    } 

} 

請告訴我如何我可以使用listview填充json數據,這是什麼問題?

+0

你如何設置適配器到你的'view',您已發佈兩次適配器,郵編你在哪裏設置適配器 – Prashant

+0

這是'NullPointerException'這意味着要麼你沒有初始化你的看法,你正在嘗試在其中填充數據。或者該佈局XML文件中不存在該視圖。或者您可能拼錯了它。 – Prashant

+0

對不起...我添加了我的MainActivity。 – Mohammadreza

回答

1

您忘記了調用setContentView()方法。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContetntView(R.layout.main_activity); 
    // rest of your code 
} 
+0

回答這個問題,我沒有注意到你已經修改了你的答案。 – Prashant

相關問題