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

Android 将对象从活动传递到非活动类,android,object,android-activity,Android,Object,Android Activity,在android上,如何将对象从活动传递到非活动类?我试图使用该类的一个实例,但当我使用一个对象(来自该类)时,我得到了NullPointerException 有人能帮我吗 提前谢谢 这是我的密码: public class IncercLaPauseActivity extends Activity { RelativeLayout layout; TextView index; @Override public void onCreate(Bundle

在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>

其中,
112行
是第一行
obj.layout.setBackgroundResource(照片[i])

您应该这样做:

IncercLaPauseActivity context ; 

public YourSecondClass(IncercLaPauseActivity _context){
   this.context = _context;
   ....
}
在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;
   ....
}
NB:您不应该实例化活动:),

编辑:

替换对象布局设置组..
。。与:

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

layout.setBackgroundResource(photo[i]);

您好,最好的方法是将对象创建为

公共类测试扩展活动{

public static Example objExml = new Example(); 
"

然后访问另一个类中的对象,如

public class TestingB extends Activity{

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

TestingA.objExml 

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

}

请发布导致出现NullPointer的代码,这将帮助您更快地获得答案。您应该将其传递给第二个类的构造函数,并确保已实例化对象。添加您的代码,以便我们可以帮助您修复:)同意上述评论-发布更多代码,明确您正在尝试执行的操作,以便我们能够最好地帮助您。那么错误在哪里??添加堆栈跟踪plz,错误在第112行,例如onTouchEvent方法,这一行是什么?但是我的第二个类扩展了ImageView并设置在xml文件上,如果我像这样更改构造函数,它将不起作用:|我通常在ex代码中给你答案,你在第二个类的构造函数上实例化你的活动,这就是为什么它不起作用的原因,因为你永远不应该用new。如果您的第二个类是imageView,那么您应该在onCreate方法上声明imageView,并将上下文传递给它的构造函数(传递这个)。。添加两个类的完整代码PLZ错误在第112行,例如在方法onTouchEvent上,这一行是什么?getParent()没有方法setBackground()…因此它不起作用。我应该修改构造函数吗?
public class TestingB extends Activity{

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

TestingA.objExml 

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

}