如何将活动的布局发送到非活动类以更改imageView?

时间:2022-08-29 09:20:43

so I'm trying to send a layout that has an imageview that I want to replace with an image picked by the user, then send that whole layout to a new activity to be displayed but I'm having trouble. It's returning null at the line below where I indicated. Please help!

所以我正在尝试发送一个具有imageview的布局,我想用用户选择的图像替换它,然后将整个布局发送到要显示的新活动但我遇到了麻烦。它在我指示的下面的行返回null。请帮忙!

So this is the class that recieves the layout and changes the image of the imageView.

因此,这是接收布局并更改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;
    }
}

Then displayImage will be called in this activity.

然后将在此活动中调用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);
    }

Finally, I want that layout (with the ImageView changed by the user) in my main layout.

最后,我希望在我的主布局中使用该布局(用户更改了ImageView)。

public class MyActivity extends Activity implements View.OnClickListener {

公共类MyActivity扩展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);

}

I also tried it this way, but the line below is always being null!!!I don't know why?!?!

我也试过这种方式,但下面的行总是为空!!!我不知道为什么?!?!

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);
    }
}

here's the xml

这是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"/>

2 个解决方案

#1


0  

Change this line of code:

更改此行代码:

  return TestingClass.displayImage(this,R.layout.user_card,testing2);

to

return TestingClass.displayImage(this,R.id.nameOfYourView,testing2);

The R.layout is your whole activity view, to find a particular view inside of it you must use R.id.

R.layout是你的整个活动视图,要查找其中的特定视图,你必须使用R.id.

For your particular case, you can create an interface or pass a list of Views too.

对于您的特定情况,您也可以创建界面或传递视图列表。

#2


0  

On your create, assign your layout to a view and get the Id from the view

在创建时,将布局分配给视图并从视图中获取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);

 }
 }

You getting null pointer cause sendstuff doesn't know where to get R.layout.user_card

你得到空指针导致sendstuff不知道从哪里得到R.layout.user_card

Change testing class to be like

改变测试类就好

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;
 } }

#1


0  

Change this line of code:

更改此行代码:

  return TestingClass.displayImage(this,R.layout.user_card,testing2);

to

return TestingClass.displayImage(this,R.id.nameOfYourView,testing2);

The R.layout is your whole activity view, to find a particular view inside of it you must use R.id.

R.layout是你的整个活动视图,要查找其中的特定视图,你必须使用R.id.

For your particular case, you can create an interface or pass a list of Views too.

对于您的特定情况,您也可以创建界面或传递视图列表。

#2


0  

On your create, assign your layout to a view and get the Id from the view

在创建时,将布局分配给视图并从视图中获取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);

 }
 }

You getting null pointer cause sendstuff doesn't know where to get R.layout.user_card

你得到空指针导致sendstuff不知道从哪里得到R.layout.user_card

Change testing class to be like

改变测试类就好

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;
 } }