2016-09-23 7 views
0

アプリがサーバーからJ sonとデータを読み取って、そのデータをカードビューで表示しても問題ありません。しかし、画像を追加してサーバーからカードビューに画像を読み込む必要があります。これを行うにはグライドライブラリを使用してください。しかし、このライブラリを追加すると、私のアプリは強制的に停止します。ここに私のコードは次のとおりです。アンドロイドスタジオでグライドが動作しない

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> 

と、この私の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" } 
] 
} 

、これが私ですexeption:

致命的な例外:メインjava.lang.IllegalArgumentException:nullにロードを開始できません。Context com.bumptech.glide.manager.RequestManagerRetriever.get(Reque stManagerRetriever.j ava:83)com.bumptech。 glide.with(Glide.java:609)com.exampleret.DataAdapter.onBindViewHolder(DataAdapter.java:44)com.example.ret.DataAdapter.onBindViewHolder(DataAdapter.jav a:19)

答えて

1

下記のフォームにごアダプタのコンストラクタを変更することができます:

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

次に、このようにそれを初期化します。

adapter = new DataAdapter(MainActivity.this, data); 
関連する問題