0

我正在处理片段类和广播接收器类。问题是,我需要将一个值从广播接收器类传递给片段类并启动片段类(如果我使用活动类,则为 startActivity(intent))。我从谷歌尝试了很多方法,但程序崩溃了。有什么办法吗?

我无法将片段更改为片段活动/其他,因为其他类受到影响。我也无法使广播接收器类分段。

这是广播接收器类:

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");

    }

和日志猫:

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)
4

1 回答 1

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");
            }
        }
    };
于 2013-10-20T06:23:07.070 回答