【问题标题】:How do I send an activity's layout to a non activity class to change an imageView?如何将活动的布局发送到非活动类以更改 imageView?
【发布时间】:2015-08-13 22:11:58
【问题描述】:

所以我试图发送一个布局,其中包含一个我想用用户选择的图像替换的图像视图,然后将整个布局发送到一个新的活动以显示,但我遇到了麻烦。它在我指出的下面的行返回 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;
    }
}

那么在这个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);
    }

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

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

}

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

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

【问题讨论】:

  • 你可以只发送图像视图而不是整个布局吗?
  • 我需要整个布局用于特定目的,有一个我没有放的arrayadapter,它需要一个布局。
  • 对不起,是的,我确实把 arrayadapter 放在了上面,它接受了一个布局
  • 有 2 个问题。用用户选择的图像切换 imageView 的图像。然后,将带有新图像的布局发送到主 Activity。

标签: android android-layout android-activity imageview


【解决方案1】:

更改这行代码:

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

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

R.layout 是您的整个活动视图,要在其中找到特定视图,您必须使用 R.id。

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

【讨论】:

  • 它仍然返回 null :( @ImageView img = (ImageView) activity.findViewById(id);
【解决方案2】:

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

 }
 }

你得到空指针导致 sendtuff 不知道从哪里得到 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;
 } }

【讨论】:

  • 但是视图不能接受只能接受 id 的布局??
  • 即使布局是relativeLayout?
  • 如果是相对布局,使用 relativelayout 代替
  • 它仍然给我一个错误,view = (LinearLayout)findViewById(R.layout.user_card);它仍然需要一个“id”而不是布局
  • 我认为你不能在视图中放入布局资源
猜你喜欢
  • 2023-04-01
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多