如何从非Activity类访问Mainactivity组件

时间:2014-10-26 04:47:16

标签: java android android-layout android-activity android-broadcast

我只想将非活动类的数据更新为Mainactivty布局组件。在我的下面的场景中,当我尝试访问Mainactivity组件(settext)时,我总是得到nullpointer异常

我的MainActivty代码:

public class MainActivity extends Activity  {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }
}

(对于上面的类我有布局,包括一些textview和EditText组件)

我的非活动课(JFYI,当我收到任何新短信时,将触发以下课程)

public class IncomingSms extends BroadcastReceiver {

String Orgi_Phonenumber;
String Orgi_MessageText;
Activity activity;
@Override
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Log.i("MyMessage", "start reading message information");
        Object[] smsobject = (Object[]) bundle.get("pdus");
        for (int i = 0; i < smsobject.length; i++) {
            SmsMessage CurrentMessage = SmsMessage.createFromPdu((byte[]) smsobject[i]);
            Orgi_Phonenumber += CurrentMessage.getOriginatingAddress();
            Orgi_MessageText += CurrentMessage.getDisplayMessageBody();
        }

    }
    Log.i("MyMessage","start updating message info " );
    changemessage();
}
public void changemessage()
{
    TextView tv = (TextView) activity.findViewById(R.id.EtextPhone);
    tv.setText(Orgi_Phonenumber);
}
}

来自上面的非活动类(IncomingSms),同时调用mainactivty布局组件

    TextView tv = (TextView) activity.findViewById(R.id.EtextPhone);

我正在获得nullpointer异常。请帮助我只知道android上的基础知识

0 个答案:

没有答案