我正在学习Android Glide,我正在创建一个简单的应用,该应用使用Glide可以显示从网络到图像的图像,但是每次运行此应用时它都会崩溃
MainActivity.java代码
package com.example.cs.myglideapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
private ImageView img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img1 = findViewById(R.id.img1);
// urls to show on imageview
//String[] urls = {
//"https://cdn.pixabay.com/photo/2016/03/04/01/46/bottles-1235327_960_720.jpg",
//"https://cdn.pixabay.com/photo/2017/06/24/23/41/beer-2439237_960_720.jpg"};
//loading image into ImageView
Glide.with(this)
.load("https://cdn.pixabay.com/photo/2016/03/04/01/46/bottles-1235327_960_720.jpg")
.into(img1);
}
}
答案 0 :(得分:3)
您忘记添加setContentView(R.layout.activity_main);
package com.example.cs.myglideapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
private ImageView img1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Add this line
img1 = findViewById(R.id.img1);
//loading image into ImageView
Glide.with(this).load("https://cdn.pixabay.com/photo/2016/03/04/01/46/bottles-1235327_960_720.jpg").into(img1);
}
}