我的問題是當我使用json創建回收視圖但它顯示異常任何人都可以解決這個問題與我的輝煌請幫我解決這個問題,我欣賞誰解決這個錯誤?回收利用json數據時Recycler視圖中的問題
public class MainActivity extends Activity {
Button b;
TextView tv;
ListView lists;
RecyclerView recyclerView;
List<Your> yourss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycle);
LinearLayoutManager ll = new LinearLayoutManager(MainActivity.this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(ll);
new JsonTask().execute("http://yoursubshop.com/webservices/categories.php");
}
public class JsonTask extends AsyncTask<String, String, List<Your>> {
@Override
protected List<Your> doInBackground(String... params) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(params[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String finaljson = sb.toString();
JSONObject firstobject = new JSONObject(finaljson);
JSONArray firstarray = firstobject.getJSONArray("data");
List<Your> yo = new ArrayList<>();
for (int i = 0; i < firstarray.length(); i++) {
JSONObject finalobject = firstarray.getJSONObject(i);
Your yos = new Your();
yos.category_id = finalobject.getString("category_id");
yos.category_name = finalobject.getString("category_name");
yo.add(yos);
}
return yo;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Your> result) {
super.onPostExecute(result);
Rvadapter listAdapter = new Rvadapter(MainActivity.this, yourss);
recyclerView.setAdapter(listAdapter);
}
}
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
List<Your> yours = Collections.emptyList();
Context c;
public class Myadapter extends RecyclerView.ViewHolder {
TextView id;
TextView name;
CardView cv;
public Myadapter(View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.texts);
name = (TextView) itemView.findViewById(R.id.textsa);
cv = (CardView)itemView.findViewById(R.id.card);
}
}
public Rvadapter(Context c, List<Your> yours) {
this.yours = yours;
this.c = c;
}
@Override
public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lists, parent, false);
Myadapter m = new Myadapter(v);
return m;
}
@Override
public void onBindViewHolder(Myadapter holder, int position) {
Your y = yours.get(position);
holder.id.setText(y.category_id);
holder.name.setText(y.category_name);
}
@Override
public int getItemCount() {
return yours.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Eception程序
06-28 17:03:34.078 802-802/com.example.anilkumar.json E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.anilkumar.json.MainActivity$Rvadapter.getItemCount(MainActivity.java:154)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3170)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2782)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
lists.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/card">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/texts"
android:text="id"
android:padding="20dp"
android:layout_margin="20sp"
android:textSize="20sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsa"
android:layout_margin="20sp"
android:text="Resturants"
android:textSize="20sp"/>
</android.support.v7.widget.CardView>
錯誤解決,但應用不列入喪生,但數據並沒有顯示在屏幕上,這是什麼錯誤 –
那麼這意味着你沒有添加正確的項目到您的適配器。嘗試將您從onPostExecute(列表結果)獲得的結果添加到適配器,如#Drv所述。 –
toshkinl
yess工作,但cardview之間很多的差距之間發生的項目之間我怎麼能解決這一個 –