【问题标题】:Java Android, How to change images from separate classJava Android,如何从单独的类更改图像
【发布时间】:2014-03-24 07:49:12
【问题描述】:

请原谅我在 Java 方面缺乏经验,到目前为止我才研究了几个星期。 我正在尝试创建一个可以根据通过 int[] 类型的 id 更改图像的类。

这是我的代码: 主 Java 文件

package com.example.imagechangeovertest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class StartActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        //When image is clicked, trigger StartIt()
        View card_holder = findViewById(R.id.ImageToAlsoChange);
        card_holder.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                startIt();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start, menu);
        return true;
    }


    //These are the images to change
    public int[] imgIds = {R.id.imageToChange, R.id.ImageToAlsoChange};

    public void startIt(){

        //Set the object up
        ImageReplacer ir = new ImageReplacer();

        //Change the images
        ir.changeImages(imgIds);

    }

}

这是课

package com.example.imagechangeovertest;

import android.widget.ImageView;

public class ImageReplacer extends StartActivity{

    public ImageReplacer(){

        //TODO - Select random images to replace the images in the layout 

    }

    public void changeImages(int[] image_ids){

        //final <Integer> = new <Integer> changeling;

        for(int i=0; i < 1;i++){//image_ids.length; i++){

            //The image to change
            int changeling = image_ids[i];

            //Find the imageView in the layout - This is where the error occurs NullPointerException
            ImageView image = (ImageView) findViewById(changeling);


            //Change the image 
            image.setImageResource(R.drawable.ti_ti);

        }

    }

}

问题是,如果我这样做,当我按下图像时,我会收到 NullPointerException 并且应用程序会关闭。 如果我从同一个班级调用它,我可以完成这项工作,只是不是这样。我也不确定我是否想让该类扩展启动器活动,但他是让它运行的唯一方法。

我做错了什么?我错过了什么?

谢谢, 克里斯

【问题讨论】:

  • 你想从哪里获取 int[] 数据?如果您正在寻找,则使用 Intents 在活动之间传递数据
  • public int[] imgIds = {R.id.imageToChange, R.id.ImageToAlsoChange};这可能在一个引用一系列不同图像的活动中发生 4 次或更多次。执行此操作的过程还将创建一些随机数据,这些数据将在其他地方使用。
  • 好的。这些图像在哪里?你从某个地方选择它们吗?请具体说明问题。你到底想做什么?
  • 对不起,我误按了返回并编辑了它。所以我在一个活动中有四张图片四次。它们是有节奏的模式,尽管这并不重要。我将在不同的活动中生成这些节奏模式,这就是我想使用对象的原因。
  • 好的。但目前还不清楚。您有 4 张图片。你以某种方式生成它们(我假设)。那么你想将基于某个事件的 id 传递给另一个 Activity 吗?

标签: java android class imageview


【解决方案1】:

不要像这样实例化活动:

    //Set the object up
    ImageReplacer ir = new ImageReplacer();

如果你想启动一个新的 Activity,你应该使用 startActivity(..) 方法:

    startActivity(this, ImageReplacer.class);

但在您的情况下,将图像替换方法移至主要活动似乎更有意义。或者有什么阻止你这样做?

或者将 ImageReplacer Activity 设置为 Main Activity(首先启动的那个),那么您将在您的 Activity 中使用该方法,而无需为此启动另一个 Activity。

更新: 如果您想要具有您的图像处理方法的不同活动,您可以像这样调整您的代码:

使 ImageReplacer 扩展 Activity:

 public class ImageReplacer extends Activity {
 // your image-handling code here

这将是您的图像相关活动的基类!

那么你的 StartActivity 应该扩展 ImageReplacer:

 public class StartActivity extends ImageReplacer {
 // your StartActivity will have image handling code inherited from ImageReplacer now
 // no need to instantiate or start another Activity for that

事实上,我建议将您的类重命名为 ImageReplacerActivity。这样就更清楚它是一个活动。

【讨论】:

  • 哦,有什么区别?我想把它放在一个单独的类中,这样我就可以在不同的活动中使用它(尽管我认为使用扩展是不可能的)。
  • 是的,你可以使用扩展,但我会让它有点不同,让我更新答案..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多