4

このクラスのコンストラクターにReceivedChat.javaというnoramljavaクラスが1つあり、Androidのアクティビティを呼び出します。

public class ReceivedChat {
    String message;
    String from;
    Context context;

    ReceivedChat(String message, String from) {
        this.message = message;
        this.from = from;

        Bundle b = new Bundle();
        b.putString("message", this.message);
        b.putString("from", this.from);
        b.putString("fromChat", "true");

        Intent i = new Intent(context.getApplicationContext(), XmppChatActivity.class);
        i.putExtras(b);
        context.getApplicationContext().startActivity(i);
    }
}

私のアクティビティクラスはXmppChatActivityです。

このプログラムは機能していません。私のクラスのonCreateを呼び出していXmppChatActivityません。どんな助けでも私に感謝します。

4

1 に答える 1

14

通常のJavaクラスからActivityクラスを呼び出す方法

ReceivedChatアクティビティまたは他のアプリケーション コンポーネントからのオブジェクト作成時に、現在のアクティビティ コンテキストを次のように渡す必要があります。

ReceivedChat(String message, String from,Context context)
{
this.message = message;
this.from = from;
this.context=context;  //<< initialize Context here 
Intent i = new Intent(context,XmppChatActivity.class);
 //....your code here
context.startActivity(i);

}

クラスコンストラクターから別のアクティビティを開始する代わりに、メソッドを作成し、ReceivedChatオブジェクトの作成後に呼び出します

于 2013-03-11T06:59:42.697 に答える