java.lang.IllegalArgumentException:目标不能为null
当我将图像url传递给毕加索时,它会向我显示错误“Target不能为null”。当我使用毕加索从url获取图像时,它会显示错误。甚至我分配给图像的id也是正确的。最后,我用静态图像替换了图像url,尽管我收到了相同的错误 这是我的密码:java.lang.IllegalArgumentException:目标不能为null,java,android,picasso,Java,Android,Picasso,当我将图像url传递给毕加索时,它会向我显示错误“Target不能为null”。当我使用毕加索从url获取图像时,它会显示错误。甚至我分配给图像的id也是正确的。最后,我用静态图像替换了图像url,尽管我收到了相同的错误 这是我的密码: public class MainScreen extends AppCompatActivity { private ImageView user_profile_pic,img2; private ImageView like_button,dislike
public class MainScreen extends AppCompatActivity {
private ImageView user_profile_pic,img2;
private ImageView like_button,dislike_button,location_button,refresh;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
private SwipeFlingAdapterView flingContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//Setting
user_profile_pic=(ImageView)findViewById(R.id.profile_image);
img2=(ImageView)findViewById(R.id.img2);
//Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg").into(user_profile_pic);
//Picasso.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);
Picasso.with(getApplicationContext()).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.custom_user_details, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Left !",Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(getApplicationContext(),"Right !",Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
@Override
public void onScroll(float scrollProgressPercent) {
try
{
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
catch (NullPointerException e) {
Log.e("tag", "NullPointerException" + e);
}
}
});
//flingContainer.getTopCardListener().selectRight(); To remove view from right side while pressing button
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
公共类主屏幕扩展AppCompative活动{
私有ImageView用户配置文件图片,img2;
私有图像查看,如_按钮、不喜欢_按钮、位置_按钮、刷新;
私人ArrayList al;
专用阵列适配器阵列适配器;
私人互联网i;
私人SwipeFlingAdapterView flingContainer;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主屏幕);
//背景
用户_profile_pic=(ImageView)findViewById(R.id.profile_image);
img2=(ImageView)findViewById(R.id.img2);
//Picasso.with(getApplicationContext()).load(“https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg)输入(用户配置文件图片);
//毕加索.with(getApplicationContext()).load(android.R.drawable.btn_star).into(img);
Picasso.with(getApplicationContext()).load(“https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
flingContainer=(SwipeFlingAdapterView)findViewById(R.id.frame);
al=新的ArrayList();
al.添加(“php”);
al.添加(“c”);
al.添加(“python”);
al.添加(“java”);
al.添加(“html”);
添加(“c++”);
al.添加(“css”);
添加(“javascript”);
arrayAdapter=新的arrayAdapter(this,R.layout.custom_user_details,R.id.helloText,al);
flingContainer.setAdapter(阵列适配器);
flingContainer.setFlingListener(新的SwipeFlingAdapterView.onFlingListener(){
@凌驾
public void removeFirstObjectInAdapter(){
Log.d(“列表”,“删除对象!”);
al.移除(0);
arrayAdapter.notifyDataSetChanged();
}
@凌驾
public void onLeftCardExit(对象dataObject){
Toast.makeText(getApplicationContext(),“Left!”,Toast.LENGTH\u SHORT.show();
}
@凌驾
RightCardExit上的公共无效(对象dataObject){
Toast.makeText(getApplicationContext(),“Right!”,Toast.LENGTH\u SHORT.show();
}
@凌驾
dapterabouttoempty上的公共void(int itemsInAdapter){
al.add(“XML.concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
日志d(“列表”、“通知”);
i++;
}
@凌驾
公共无效onScroll(浮动百分比){
尝试
{
View=flingContainer.getSelectedView();
view.findViewById(R.id.item\u swipe\u right\u indicator).setAlpha(scrollProgressPercent<0?-scrollProgressPercent:0);
view.findViewById(R.id.item\u swipe\u left\u indicator).setAlpha(scrollProgressPercent>0?scrollProgressPercent:0);
}
捕获(NullPointerException e){
Log.e(“标记”、“NullPointerException”+e);
}
}
});
//flingContainer.getTopCardListener()。选择right();可在按下按钮的同时从右侧移除视图
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单主屏幕,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
这就是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/img2"
android:src="@android:drawable/btn_star"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:padding="8dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vimal, "
android:id="@+id/helloText"
android:textColor="@android:color/black"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"
android:textColor="@android:color/black"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@android:drawable/btn_dialog"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="25sp"
android:layout_marginLeft="5dp"
android:layout_gravity="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="#A5F" />
<View
android:id="@+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:layout_gravity="right"
android:background="#5AF" />
</FrameLayout>
img2
为空,您正在将其传递到函数中
在img2=(ImageView)findViewById(R.id.img2)之后添加空检查
检查它何时停止为null,或者在测试时是否保持null,或者只是确认它为null
检查布局ID,确保它们适用于所有配置,并确保指定了@+ID/img2
。是否确实加载了正确的布局?因为您在Java文件中有R.id.profile\u图像,但我在您的xml文件中没有找到该id。尝试将其更改为
Picasso.with(MainScreen.this).load("https://pbs.twimg.com/profile_images/596282530652753921/bPf8NmOs.jpg")
.placeholder(android.R.drawable.btn_star)
.error(android.R.drawable.btn_star)
.into(img2);
还要确保您使用的是activity\u main\u screen.xml
布局,如果有img2,请检查id。您不能使用毕加索库从https下载图像。
您可以使用库。android.R.drawable.btn\u star?是的@Rustam默认使用android的图像。img2是目标,所以您是否检查了它是否为空?您使用的布局是否正确?如果是,请添加活动代码。只需清理并再次运行我尝试使用以下代码检查空值:if(img2==null){img2.setImageResource(android.R.drawable.btn_star);}else{Picasso.with(getApplicationContext()).load(“).placeholder(android.R.drawable.btn_star).error(android.R.drawable.btn_star).into(img2);}然后我在logcat中收到了这个错误:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)'