将对象从活动传递到非活动类

时间:2011-10-21 14:06:42

标签: android object android-activity

如何在android上将对象从活动传递给非活动类?我尝试使用该类的实例,但是当我使用一个对象(来自该类)时,我得到NullPointerException

任何人都可以帮助我吗?

提前致谢。

这是我的代码:

public class IncercLaPauseActivity extends Activity {

    RelativeLayout layout;
    TextView index;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        layout = (RelativeLayout) findViewById(R.id.imagBackground);
        index = (TextView) findViewById(R.id.index);
        }
}

xml文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/photo0"
    android:id="@+id/imagBackground">

    <TextView android:layout_width="20dip" android:layout_height="20dip"
        android:layout_marginLeft="250dip" android:layout_marginTop="15dip"
        android:background="#FFFFFF" android:id="@+id/index" android:text="0"
        android:textColor="#000000">
    </TextView>
    <com.www.MyImageView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/myimage"
        android:layout_below="@id/index">
    </com.www.MyImageView>

</RelativeLayout>

在非活动课上我会这样做:

public class MyImageView extends ImageView {

    Paint rectanglePaint;
    int x, y,i=0;
    Rect tete, yeux, dos, abdomen, epaule1, epaule2, avant_bras1, avant_bras2,
            main1, main2, genoux, pieds, respiration;
    Bitmap bit;

    IncercLaPauseActivity obj;

    private int[] photos = { R.drawable.photo0, R.drawable.photo1,
            R.drawable.photo2, R.drawable.photo3, R.drawable.photo4,
            R.drawable.photo5, R.drawable.photo6, R.drawable.photo7,
            R.drawable.photo8, R.drawable.photo9, R.drawable.photo10,
            R.drawable.photo11, R.drawable.photo12, R.drawable.photo13,
            R.drawable.photo14, R.drawable.photo15, R.drawable.photo16,
            R.drawable.photo17, R.drawable.photo18, R.drawable.photo19,
            R.drawable.photo20, R.drawable.photo21, R.drawable.photo22 };

    public MyImageView(Context context) {
        super(context);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        rectanglePaint = new Paint();
        rectanglePaint.setARGB(120, 255, 0, 0);
        tete = new Rect(220, 0, 280, 67);// in pixels

        yeux = new Rect(220, 4, 280, 20);

        epaule1 = new Rect(175, 75, 200, 197);
        epaule2 = new Rect(297, 75, 320, 197);

        avant_bras1 = new Rect(170, 200, 195, 300);// in pixels
        avant_bras2 = new Rect(300, 200, 325, 300);// in pixels

        main1 = new Rect(170, 303, 195, 345);
        main2 = new Rect(300, 303, 325, 345);

        respiration = new Rect(200, 75, 297, 177);
        abdomen = new Rect(202, 177, 290, 260);
        genoux = new Rect(200, 260, 297, 430);
        bit = BitmapFactory.decodeResource(getResources(),
                   R.drawable.anim_ctrl_panel);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        System.out.println("draw");
        canvas.drawRect(tete, rectanglePaint);

        canvas.drawRect(epaule1, rectanglePaint);
        canvas.drawRect(epaule2, rectanglePaint);

        rectanglePaint.setARGB(120, 0, 255, 0);
        canvas.drawRect(avant_bras1, rectanglePaint);
        canvas.drawRect(avant_bras2, rectanglePaint);

        rectanglePaint.setARGB(120, 0, 0, 255);
        canvas.drawRect(main1, rectanglePaint);
        canvas.drawRect(main2, rectanglePaint);

        rectanglePaint.setARGB(120, 0, 255, 255);
        canvas.drawRect(respiration, rectanglePaint);

        rectanglePaint.setARGB(120, 255, 0, 255);
        canvas.drawRect(abdomen, rectanglePaint);
        canvas.drawRect(yeux, rectanglePaint);

        rectanglePaint.setARGB(120, 255, 255, 0);
        canvas.drawRect(genoux, rectanglePaint);


        canvas.drawBitmap(bit, 80, 410, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = (int) event.getX();
        System.out.println("x este " + x);
        y = (int) event.getY();
        System.out.println("y este " + y);
        if (avant_bras1.contains(x, y) || (avant_bras2.contains(x, y))) {
            System.out.println("antebrat");
            Intent intent = new Intent();
            intent.setClass(this.getContext(), Selecter.class);
            this.getContext().startActivity(intent);
        }

        if (event.getX() > bit.getWidth() / 2) {
            System.out.println("right");
            i++;
            System.out.println("i=" + i);
            if (i <= 22) {
                obj.layout.setBackgroundResource(photos[i]);
                obj.index.setText(String.valueOf(i));
            } else {
                i = 0;
                obj.layout.setBackgroundResource(photos[i]);
                obj.index.setText(String.valueOf(i));
            }
        }else {
            i--;
            System.out.println("i=" + i);
            if (i >= 0) {
                obj.layout.setBackgroundResource(photos[i]);
                obj.index.setText(String.valueOf(i));
            } else {
                i = 22;
                obj.layout.setBackgroundResource(photos[i]);
                obj.index.setText(String.valueOf(i));
            }
        }
        System.out.println("left");
        return false;
    }

}

我收到此错误:

10-21 17:34:42.825: ERROR/AndroidRuntime(2011): java.lang.NullPointerException
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at com.www.MyImageView.onTouchEvent(MyImageView.java:112)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
10-21 17:34:42.825: ERROR/AndroidRuntime(2011):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)

其中line 112是第一行obj.layout.setBackgroundResource(photos[i]);

2 个答案:

答案 0 :(得分:0)

你应该这样做:

在onCreate方法上

添加:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        layout = (RelativeLayout) findViewById(R.id.imagBackground);
        index = (TextView) findViewById(R.id.index);
        //here pass your activity ( this ) to your second Class and instanciate it 
        YourSecondClass obj = new YourSecondClass(this);
        }

在你的第二节课中,构造函数将是这样的:

IncercLaPauseActivity context ; 

public YourSecondClass(IncercLaPauseActivity _context){
   this.context = _context;
   ....
}

注意:你永远不应该实施一项活动:),

编辑:

obj.layout.setBakgroun.. ..替换为:

RelativeLayour layout = (RelativeLayout) this.getParent();

layout.setBackgroundResource(photo[i]);

答案 1 :(得分:0)

嗨,最好的方法是创建一个对象

  

public class TestingA extends Activity {   ........................................

public static Example objExml = new Example(); 
     

.........................................}

然后在

下访问另一个类中的对象
public class TestingB extends Activity{

...................................
your code
................................

TestingA.objExml 

...............................

}