Android:片段类的startActivity(intent)

时间:2013-10-20 05:55:59

标签: android android-fragments broadcastreceiver

我正在处理片段类和broadcastReceiver类。问题是,我需要将一个值从broadcastreceiver类传递给片段类并启动片段类(如果我使用活动类,则启动startActivity(intent))。我尝试了很多来自Google的方法,但程序崩溃了。有没有办法做到这一点?

我无法将片段更改为fragmentActivity / others,因为其他类会受到影响。 我不能使broadcastReceiver类也碎片化。

这是broadcastreceiver类:

Fragmented fragment = new Fragmented();
  Bundle bundle2 = new Bundle();
            bundle2.putString("key", key);
            fragment.setArguments(bundle2);

        fragment.onStart();

这是片段类:

@Override
    public void onStart()
    {

        Bundle bundle = this.getArguments();
              String hello = bundle.getString("key");
              System.out.println(hello);

            super.onStart();

            FlurryAgent.onStartSession(getActivity().getApplicationContext(),"xxxx");
        EasyTracker.getInstance().activityStart(getActivity());
        EasyTracker.getTracker().sendView("(2.0)New_MainView");

    }

和logcat:

E/AndroidRuntime(16572): java.lang.RuntimeException: Unable to start receiver com.XX.XX.Receiver: java.lang.NullPointerException 10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.app.ActivityThread.access$2400(ActivityThread.java:117) 10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.os.Handler.dispatchMessage(Handler.java:99)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.os.Looper.loop(Looper.java:130)10-20 
13:20:30.790: E/AndroidRuntime(16572):  at android.app.ActivityThread.main(ActivityThread.java:3691)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at java.lang.reflect.Method.invokeNative(Native Method)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at java.lang.reflect.Method.invoke(Method.java:507)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at dalvik.system.NativeStart.main(Native Method)10-20 

13:20:30.790: E/AndroidRuntime(16572): Caused by: java.lang.NullPointerException10-20 

13:20:30.790: E/AndroidRuntime(16572):  at com.XX.XX.Fragmented.onStart(Fragmented.java:265)10-20 

 13:20:30.790: E/AndroidRuntime(16572):     at com.xx.xx.Receiver.onReceive(Receiver.java:84)10-20 

13:20:30.790: E/AndroidRuntime(16572):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)

1 个答案:

答案 0 :(得分:1)

在接收器类中定义此方法。并调用此方法通知活动。

public static final String YOUR_KEY = "yourbackagename.YOUR_KEY";
void broadcastMessage(Context context, String message) {
    Intent intent = new Intent(YOUR_KEY);
    intent.putString("Key", message);
    context.sendBroadcast(intent);
}

在您的活动中创建BroadcastReceiver并监听广播。此活动应该是您应该保留片段的主要活动。

registerReceiver(reciever, new IntentFilter(YourReciever.YOUR_KEY));

final BroadcastReceiver reciever = new BroadcastReceiver() {
        /*
         * (non-Javadoc)
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
         * @since Oct 7, 2013
         * @author rajeshcp
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            try{
                Fragmented fragment = new Fragmented();
                            fragment.setArguments(intent.getExtras());
                            FragmentTransaction trasaction = getSupportFragmentManager().beginTransaction();
                            //R.id.fragment_content The id given to the FrameLayout in your main lay out fro the Activity
                            trasaction.replace(R.id.fragment_content, fragment, getSupportFragmentManager().getBackStackEntryCount() + "");
                    trasaction.addToBackStack(null);
                    trasaction.commit();
            }catch(Exception exception){
                Log.d(getClass().getName(), "Error on recieve of notification");
            }
        }
    };