我的活动没有开始

时间:2016-09-29 17:40:14

标签: android android-fragments android-activity

我的问题如下: 我有一个活动(MainActivity),里面显示一个片段(MainFragment)。在MainFragment中,有一个FloatingActionButton调用一个新活动(NewStudentActivity)。问题是,我点击了FAB,没有任何反应!奇怪的是,在内存监视器中,如果我一直按下FAB,它会继续上升,就像创建活动但从未显示过一样。这是MainFragment和NewStudentActivity的代码:

CREATE OR REPLACE FUNCTION fnc()
   RETURNs integer
AS
$$
declare
   tb_var adm_applications[];
begin
   return 1;
end;
$$
language plpgsql;

是的,所有活动都在清单中声明。

正如你们有些人问的那样,这里是CalcMethodFragment代码(它只是一个DialogFragment):

public class MainFragment extends Fragment {

private View fragmentView;

private FloatingActionButton fab;
private ListView listView;
private StudentAdapter listViewAdapter;

private ArrayList<Student> studentList = new ArrayList<>();

public static MainFragment newInstance() {
    return new MainFragment();
}

public MainFragment(){
    //Required empty constructor
}

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

    fragmentView = inflater.inflate(R.layout.fragment_main, container, false);

    fab = (FloatingActionButton) fragmentView.findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(getActivity(), NewStudentActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
        }
    });

    listView = (ListView) fragmentView.findViewById(R.id.list_view_students);

    listViewAdapter = new StudentAdapter(getActivity().getApplicationContext(), studentList);
    prepareStudentData(getStudents());
    listView.setAdapter(listViewAdapter);



    return fragmentView;
}

//-----------------NewStudentActivity---------------------


public class NewStudentActivity extends AppCompatActivity implements CalcMethodFragment.SelectCalcOptionDialogListener,
    UserGenderFragment.SelectGenderOptionDialogListener{

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int calcMethod;
private int userGender;

public interface StudentCreatedListener{
    void onStudentCreated();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_student);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    CalcMethodFragment frag = new CalcMethodFragment();
    frag.show(ft, "calc_method");
}

2 个答案:

答案 0 :(得分:1)

您可以试试这个:

,而不是使用FragmentTransaction节目
CalcMethodFragment dialog = new CalcMethodFragment ();
dialog.show(getSupportFragmentManager(), "calc_method");

答案 1 :(得分:0)

尝试更改
Intent i = new Intent(getActivity(), NewStudentActivity.class);
Intent i = new Intent(getContext(), NewStudentActivity.class); getContext().startActivity(i);

有助于查看您的CalcMethodFrag#show()方法