我正在尝试使用Picasso API for Android Studio Java从URL下载图像。单击浮动操作栏,将显示一个警告对话框,其中包含URL中的图像。
我试过以下事项: Picasso Target must not be null error。 但我的错误仍在发生。
这是我目前正在使用的代码:
FloatingActionButton FabViewImage = (FloatingActionButton) findViewById(R.id.fabPicture);
if (FabViewImage != null)
FabViewImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog settingsDialog = new Dialog(TabEmpOrder.this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.order_detail_image_view, null));
Context context = settingsDialog.getContext();
settingsDialog.show();
ImageView imgView = (ImageView) findViewById(R.id.imgOrderDetails);
Picasso.with(context).load("http://i.imgur.com/Tnendik.png").into(imgView);
}
});
这是显示的错误消息
08-23 09:54:06.351 25488-25488/dura_art.bigbyte E/AndroidRuntime: FATAL EXCEPTION: main
Process: dura_art.bigbyte, PID: 25488
java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at dura_art.bigbyte.TabEmpOrder$1.onClick(TabEmpOrder.java:99)//This is where it breaks
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我哪里出错了?我该如何解决?
答案 0 :(得分:0)
这样做
View v = getLayoutInflater().inflate(R.layout.order_detail_image_view, null);
settingsDialog.setContentView(v);
Context context = settingsDialog.getContext();
settingsDialog.show();
ImageView imgView = (ImageView) v.findViewById(R.id.imgOrderDetails);
Picasso.with(context).load("http://i.imgur.com/Tnendik.png").into(imgView);
答案 1 :(得分:0)
您必须从用于ImageView
的布局中获取AlertDialog
,请尝试以下代码:
@Override
public void onClick(View view) {
Dialog settingsDialog = new Dialog(TabEmpOrder.this);
settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View layout = getLayoutInflater().inflate(R.layout.order_detail_image_view;
ImageView imgView = (ImageView) layout.findViewById(R.id.imgOrderDetails);
settingsDialog.setContentView(layout, null));
Context context = settingsDialog.getContext();
settingsDialog.show();
Picasso.with(context).load("http://i.imgur.com/Tnendik.png").into(imgView);
}
传递给ImageView
时null
picasso
为function startGame() {
game.start();
game.createSquare('red', 100, 10, 10, 10);
}
function updateGame() {
game.clear();
//lines below are causing issues
game.x += game.delta; // set new position
// if rectangle touches right side (remember about the width of rectangle),
// or touches left side of canvas, revert direction
if(game.x > game.canvas.width - game.width || game.x <= 0){
game.delta *= -1;
}
game.update();
}
var game = {
//create the canvas element
canvas: document.createElement('canvas'),
//set up the canvas
start: function () {
this.context = this.canvas.getContext('2d');
this.canvas.height = 400;
this.canvas.width = 400;
document.body.insertBefore(this.canvas ,document.body.childNodes[0]);
},
//clear the canvas
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
//create Square
createSquare: function(color, height, width, x, y) {
this.height = height;
this.width = width;
this.color = color;
this.x = x;
this.y = y;
this.delta = 1; // this is speed and direction of movement
this.update = function(){
this.context.fillStyle = this.color;
this.context.fillRect(this.x , this.y , this.width, this.height);
}
}
}
startGame();
setInterval(updateGame,50);
,但现在却不可能。