所以我正在尝试发送一个具有imageview的布局,我想用用户选择的图像替换它,然后将整个布局发送到要显示的新活动但我遇到了麻烦。它在我指示的下面的行返回null。请帮忙!
因此,这是接收布局并更改imageView图像的类。
public class TestingClass
{
public static int displayImage(Activity activity, int id, String path)
{
ImageView img = (ImageView) activity.findViewById(id);//This is where it's returning null
img.setImageBitmap(BitmapUtility.decodeSampledBitmapFromResource(path,500,500));
return id;
}
}
然后将在此活动中调用displayImage。
public class UserCard extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.user_card);
}
public int sendstuff()
{
String testing2 = cardFactory.getInstance().getValue().grabImagePathString;//Getting the image path the user selected
return TestingClass.displayImage(this,R.layout.user_card,testing2);
}
最后,我希望在我的主布局中使用该布局(用户更改了ImageView)。
public class MyActivity extends Activity实现了View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState)//Does Load METHODS in order!!!!
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButterKnife.inject(this);
UserCard user = new UserCard();
al = new ArrayList<>();
al.add("max");
arrayAdapter = new ArrayAdapter<>(this, user.sendstuff(), R.id.helloText, al);
flingContainer.setAdapter(arrayAdapter);
}
我也尝试过这种方式,但下面的行总是为空!!!我不知道为什么?!?!
public class UserCard extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.item);
}
public int displayImage(String path)
{
ImageView img = (ImageView) findViewById(R.id.maxCard);// THIS IS RETURNING NULL ALWAYLS ?!?!
img.setImageBitmap(BitmapUtility.decodeSampledBitmapFromResource(path,500,500));
return R.layout.item;
}
public int sendstuff(String path)//In my other activity this is method i make an instance of, and I enter an image path string.
{
return displayImage(path);
}
}
这是xml
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:clickable="false"
android:id="@+id/CardView">
<RelativeLayout
android:layout_width="210dp"
android:layout_height="354dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/myCardFrame">
<ImageView
android:layout_width="210dp"
android:layout_height="254dp"
android:id="@+id/imageButtonQ"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:cropToPadding="false"
android:src="@drawable/max"/>
答案 0 :(得分:0)
更改此行代码:
return TestingClass.displayImage(this,R.layout.user_card,testing2);
到
return TestingClass.displayImage(this,R.id.nameOfYourView,testing2);
R.layout是你的整个活动视图,要查找其中的特定视图,你必须使用R.id。
对于您的特定情况,您也可以创建界面或传递视图列表。
答案 1 :(得分:0)
在创建时,将布局分配给视图并从视图中获取ID
public class UserCard extends Activity {
View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = View.inflate(this, R.layout.user_card, null);
setContentView(view);
}
public int sendstuff() {
String testing2 = cardFactory.getInstance().getValue().grabImagePathString;//Getting the image path the user selected return
TestingClass.displayImage(view ,testing2);
}
}
你得到空指针导致sendstuff不知道从哪里得到R.layout.user_card
将测试类更改为
public class TestingClass {
public static int displayImage(View view, String path) {
ImageView img = (ImageView) view.findViewById(R.id.imageButtonQ);//This is where it's returning null img.setImageBitmap(BitmapUtility.decodeSampledBitmapFromResource(path,500,500));
return id;
} }