erfan erfan - 2 months ago 9
JSON Question

my json codes work fine but when add Glide for parsing images app force stope

app read data with J son from server and show that data in card view and all things work fine .but i want add image and read image from server to in card view . use Glide library for do this but when add this library my app become to force stop . here is my code :

MainActivity :

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ArrayList<AndroidVersion> data;
private DataAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
initViews();
}
private void initViews(){
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://memaraneha.ir/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}

@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
}


JSONResponse:

public class JSONResponse {
private AndroidVersion[] android;

public AndroidVersion[] getAndroid() {
return android;
}
}


DataAdapter:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private Context context;
private ArrayList<AndroidVersion> android;


public DataAdapter(ArrayList<AndroidVersion> android) {
this.android = android;
}


@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

viewHolder.tv_name.setText(android.get(i).getName());
viewHolder.tv_version.setText(android.get(i).getVer());
viewHolder.tv_api_level.setText(android.get(i).getApi());


// load image into imageview using glide
Glide.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic())
.placeholder(R.drawable.truiton)
.error(R.drawable.truiton)
.into(viewHolder.tv_image);
}

@Override
public int getItemCount() {
return android.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ImageView tv_image;
public ViewHolder(View view) {
super(view);

tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
tv_image= (ImageView) view.findViewById(R.id.img);

}
}

}


AndroidVersion:

public class AndroidVersion {
private String ver;
private String name;
private String api;
private String pic;

public String getVer() {
return ver;
}

public String getName() {
return name;
}

public String getApi() {
return api;
}
public String getPic(){
return pic;
}
}


RequestInterface:

public interface RequestInterface {

@GET("Erfan/ret.php")
Call<JSONResponse> getJSON();
}


card_row.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
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_api_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
</android.support.v7.widget.CardView>

</LinearLayout>


main_page.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.RecyclerView
android:id="@+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>


and this my json :

{ "android":
[
{ "ver": "1.5", "name": "Cupcake", "api": "API level 3","pic":"pic1.jpg" },
{ "ver": "1.6", "name": "Donut", "api": "API level 4","pic":"pic2.jpg" },
{ "ver": "2.0 - 2.1", "name": "Eclair", "api": "API level 5 - 7","pic":"pic3.jpg" },
{ "ver": "2.2", "name": "Froyo", "api": "API level 8","pic":"pic4.jpg" },
{ "ver": "2.3", "name": "Gingerbread", "api": "API level 9 - 10","pic":"pic5.jpg" },
{ "ver": "3.0 - 3.2", "name": "Honeycomb", "api": "API level 11 - 13","pic":"pic6.jpg" },
{ "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14 - 15","pic":"pic7.jpg" },
{ "ver": "4.1 - 4.3", "name": "JellyBean", "api": "API level 16 - 18","pic":"pic8.jpg" },
{ "ver": "4.4", "name": "KitKat", "api": "API level 19","pic":"pic9.jpg" },
{ "ver": "5.0 - 5.1", "name": "Lollipop", "api": "API level 21 - 22","pic":"pic10.jpg" },
{ "ver": "6.0", "name": "Marshmallow", "api": "API level 23","pic":"pic11.jpg" }
]
}


i think my problem is in Data Adapter when add Glide ... if some one can please help me

Answer

Change your adapter's Constructor and pass Context to it. As your log shows, Glide want to access to Context but it's not initialized.

  public DataAdapter(Contenxt context,ArrayList<AndroidVersion> android) {
        this.context = context;
        this.android = android;
    }

initialize your adapter with:

adapter = new DataAdapter(MainActivity.this,data);