应用程序崩溃,同时将一个片段转移到另一个片段

时间:2016-09-04 17:03:24

标签: android android-fragments

我正试图从一个片段跳到另一个片段。但每当我点击按钮跳转第二个片段我的应用程序崩溃。 这是我的源文件。请帮忙 ! 我想将Tab1导航到AlertScreen 这是Tab1.java

package com.example.bavarian.sos;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;




public class Tab1 extends Fragment {
    ImageButton HelpButton ;
    private boolean playing = false;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab1, container, false);
        HelpButton = (ImageButton) rootView.findViewById(R.id.imageButton);
        final MediaPlayer Alarm = MediaPlayer.create(getContext(),R.raw.sound);
        //private boolean playing = false ;
        HelpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new AlertScreen();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }

        });

        return rootView ;
    }
}

AlertScreen.java

package com.example.bavarian.sos;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AlertScreen extends Fragment {
public AlertScreen(){

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.alert_screen,container,false);
    return rootView;
}
}

alert_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Alerrt Screen"
    android:id="@+id/alerttext"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
     android:layout_marginTop="176dp" />
  </RelativeLayout>

我在这里缺少什么?

0 个答案:

没有答案