Java 我正在使用碎片,当我快速按下后退按钮时,应用程序崩溃。这是随机发生的

Java 我正在使用碎片,当我快速按下后退按钮时,应用程序崩溃。这是随机发生的,java,android,fragment,Java,Android,Fragment,我使用的是碎片,当我快速按下后退按钮时,android应用程序崩溃了。这是随机发生的,下面是java代码: public class PlayerFragment extends Fragment { List<County> feeds; String action = "counties"; private static MediaPlayer mMediaPlayer; TextView descr; TextView feed_id;

我使用的是碎片,当我快速按下后退按钮时,android应用程序崩溃了。这是随机发生的,下面是java代码:

public class PlayerFragment extends Fragment {
    List<County> feeds;
    String action = "counties";
    private static MediaPlayer mMediaPlayer;

    TextView descr;
    TextView feed_id;
    TextView status;
    TextView listeners;
    TextView genre;
    TextView bitrate;

    private static Button switchButton;

    private AdView mAdView;
    InterstitialAd mInterstitialAd;

    public PlayerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_player, container, false);
        //view called and initialized
        initView(rootView);
        updateView();
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });

        final Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(RadioApiClient.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());
        Retrofit retrofit = builder.build();

        RadioApiClient client = retrofit.create(RadioApiClient.class);

        Call<Counties> call = client.getCounties(action,
                BrowseStateFragment.stateId,
                RadioApiClient.JSON_RESPONSE,
                RadioApiClient.API_KEY);
        Log.d("Accessing url", client.getCounties(action, BrowseStateFragment.stateId,
                RadioApiClient.JSON_RESPONSE,
                RadioApiClient.API_KEY).request().url().toString());
        call.enqueue(new Callback<Counties>() {

            @Override
            public void onResponse(Call<Counties> call, Response<Counties> response) {


                feeds = response.body().getCounties();

                AudioManager amanager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
                int maxVolume = amanager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
                amanager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0);
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        togglePlayPause();
                    }
                });

                String station = BrowseFeedsFragment.station;
                Toast.makeText(getActivity(), "Setting radio", Toast.LENGTH_LONG).show();
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                    Toast.makeText(getActivity(), "stopping radio", Toast.LENGTH_LONG).show();

                    mMediaPlayer.reset();
                    Toast.makeText(getActivity(), "resetting radio", Toast.LENGTH_LONG).show();
                }
                try {
                    Toast.makeText(getActivity(), "Trying to play ", Toast.LENGTH_LONG).show();
                    mMediaPlayer.setDataSource(station);
                    mMediaPlayer.prepareAsync();
                    mMediaPlayer.start();
                    showInterstitial();
                    Toast.makeText(getActivity(), "playing", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<Counties> call, Throwable t) {
                Log.d("no success", "" + "onfailure");
                t.printStackTrace();
            }
        });
        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
            showInterstitial();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private void togglePlayPause() {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            switchButton.setText("Play");
            Toast.makeText(getActivity(), "Paused", Toast.LENGTH_LONG).show();

        } else {
            mMediaPlayer.start();
            switchButton.setText("Pause");


            Toast.makeText(getActivity(), "Playing", Toast.LENGTH_LONG).show();

        }
    }

    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

    public void initView(View rootView) {
        descr = (TextView) rootView.findViewById(R.id.descr);
        feed_id = (TextView) rootView.findViewById(R.id.feed_id);
        status = (TextView) rootView.findViewById(R.id.status);
        listeners = (TextView) rootView.findViewById(R.id.listeners);
        genre = (TextView) rootView.findViewById(R.id.genre);
        bitrate = (TextView) rootView.findViewById(R.id.bitrate);
        switchButton = (Button) rootView.findViewById(R.id.switchButton);
        mAdView = (AdView) rootView.findViewById(R.id.adView);
        mInterstitialAd = new InterstitialAd(getActivity());

    }

    public void updateView() {
        //update views

        descr.setText("Description: " + BrowseFeedsFragment.descr);
        feed_id.setText("Feed Id: " + BrowseFeedsFragment.feed_id);

        if (BrowseFeedsFragment.status == "1") {
            status.setText("Status: " + "Live Streaming Active");
        } else {
            status.setText("Status: " + "Live Streaming Offline");
        }

        listeners.setText("Listerners: " + BrowseFeedsFragment.listeners);
        genre.setText("Genre: " + BrowseFeedsFragment.genre);
        bitrate.setText("Bitrate: " + BrowseFeedsFragment.bitrate + "KBPS");

        Log.d("Success", "" + "Update Success");

        //event handling
        switchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                togglePlayPause();
            }
        });

    }
}
我收到的错误消息如下:

> FATAL EXCEPTION: main
>     Process: com.kwawannan.policedispatchlivescanner, PID: 27704
>     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null
> object reference
>          at android.widget.Toast.<init>(Toast.java:103)
>          at android.widget.Toast.makeText(Toast.java:256)
>          at com.kwawannan.policedispatchlivescanner.activity.PlayerFragment.togglePlayPause(PlayerFragment.java:208)
>          at com.kwawannan.policedispatchlivescanner.activity.PlayerFragment.access$200(PlayerFragment.java:36)
>          at com.kwawannan.policedispatchlivescanner.activity.PlayerFragment$2$1.onPrepared(PlayerFragment.java:123)
>          at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2872)
>          at android.os.Handler.dispatchMessage(Handler.java:102)
>          at android.os.Looper.loop(Looper.java:154)
>          at android.app.ActivityThread.main(ActivityThread.java:6119)
>          at java.lang.reflect.Method.invoke(Native Method)
>          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
>          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是因为在片段被销毁并从活动中分离后调用了onResponse

正确的解决方案是在销毁片段时取消或处置调用:

onDestroyView中的call call.cancel或call.dispose方法显示错误代码

com.kwawannan.policedispatchlivescanner, PID: 27704 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.widget.Toast.(Toast.java:103)
您应该使用getContext而不是getActivity;像

Toast.makeText(getContext(), "msg text", Toast.LENGTH_LONG).show();

更改切换方法,如下所示:

private void togglePlayPause() {
        Activity activity = getActivity();
        if (mMediaPlayer!=null && activity!=null) {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
                switchButton.setText("Play");
                Toast.makeText(activity, "Paused", Toast.LENGTH_LONG).show();    
            } else {
                mMediaPlayer.start();
                switchButton.setText("Pause");
                Toast.makeText(activity, "Playing", Toast.LENGTH_LONG).show();
            }
        }
    }

我希望这将对您有所帮助。

如何使用call.cancel,call在我的代码中未知在onCreateView中您正在创建call=client.getCountie。。。只需将其设为类成员,这样您就可以在调用call后仍能访问它。取消对空对象引用的“java.lang.String android.content.Context.getPackageName”,在com.kwawannan.policydispatchlivescanner.activity.PlayerFragment.togglePlayPausePlayerFragment.java:210在com.kwawannan.policydispatchLiveScanner.activity.PlayerFragment.access$200PlayerFragment.java:36在com.kwawannan.policydispatchLiveScanner.activity.PlayerFragment$2$1.onPreparedPlayerFragment.java:124可能是它的副本-您正在向某个具有无效上下文的地方显示toast我应该使用什么上下文?