基本的には、BroadCaseReceiver 内の TextView クラスの setText() 機能を実行したいと考えています。しかし、私はこれを実装することができません:わかりません
Activityクラスの一部であり、Activity(ベースアクティビティ)クラスの参照を表示しているため、なぜ(context.findviewById()
動作していません)?findViewById()
context.toString()
基本的には、BroadCaseReceiver 内の TextView クラスの setText() 機能を実行したいと考えています。しかし、私はこれを実装することができません:わかりません
Activityクラスの一部であり、Activity(ベースアクティビティ)クラスの参照を表示しているため、なぜ(context.findviewById()
動作していません)?findViewById()
context.toString()
以下の回答へのリンクはここにあります:他のフラグメントとの通信
FragmentがそのActivityまで通信できるようにするには、Fragmentクラスでインターフェースを定義し、それをActivity内に実装できます。フラグメントは、onAttach()ライフサイクルメソッド中にインターフェイスの実装をキャプチャし、アクティビティと通信するためにインターフェイスメソッドを呼び出すことができます。
フラグメントからアクティビティへの通信の例を次に示します。
public class HeadlinesFragment extends ListFragment {OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
これで、フラグメントは、OnHeadlineSelectedListenerインターフェイスのmCallbackインスタンスを使用してonArticleSelected()メソッド(またはインターフェイス内の他のメソッド)を呼び出すことにより、アクティビティにメッセージを配信できます。
たとえば、ユーザーがリストアイテムをクリックすると、フラグメント内の次のメソッドが呼び出されます。フラグメントは、コールバックインターフェイスを使用して、イベントを親アクティビティに配信します。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
インターフェイスの実装フラグメントからイベントコールバックを受信するには、それをホストするアクティビティがフラグメントクラスで定義されたインターフェイスを実装する必要があります。
たとえば、次のアクティビティは、上記の例のインターフェイスを実装します。
public static class MainActivity extends ActivityimplementsHeadlinesFragment.OnHeadlineSelectedListener{..。
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}フラグメントへのメッセージの配信ホストアクティビティは、findFragmentById()を使用してFragmentインスタンスをキャプチャし、フラグメントのパブリックメソッドを直接呼び出すことにより、メッセージをフラグメントに配信できます。
たとえば、上記のアクティビティに、上記のコールバックメソッドで返されたデータによって指定されたアイテムを表示するために使用される別のフラグメントが含まれている可能性があるとします。この場合、アクティビティは、コールバックメソッドで受信した情報を、アイテムを表示する他のフラグメントに渡すことができます。
public static class MainActivity extends ActivityimplementsHeadlinesFragment.OnHeadlineSelectedListener{..。
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
Activity.onCreate() などのアクティビティのコンテキスト内で BroadcastReceiver がプログラムによって開始された場合を除きます。通常の BroadcastReceiver から TextView のような UI 要素にアクセスするのは非常に奇妙です。アクティビティからの UI のみを操作できます。適用しようとしているソリューションを確認する必要があるかもしれません。仮にそんなことができたとしても、それは間違った設計になってしまいます。
public class MyReceiver extends BroadcastReceiver {
private MyActivity myActivity;
public MyReceiver(MyActivity myActivity) {
this.myActivity= myActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("", "Onrecieve ready to call");
if(this.myActivity!=null)
{
this.myActivity.update();
}
// make update method is in your activity.
// call function of your activity and change UI.
}
}