简体   繁体   English

使用OnClickListener的Android应用在startActivity(intent)上关闭

[英]Android app using OnClickListener closes on startActivity(intent)

I have an OnClickListener in a fragment activity as shown here: 我在片段活动中有一个OnClickListener ,如下所示:

public class Menu1_Fragment extends Fragment {
    Button CreateGroupButton;
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.menu1_layout, container, false);
        CreateGroupButton = (Button) rootview.findViewById(R.id.create_study_group);

        CreateGroupButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Menu1_Fragment.this.getActivity(), CreateGroup.class);
                startActivity(intent);


            }
        });


        return rootview;
    }
}

Here is my menu1_layout.xml code: 这是我的menu1_layout.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"
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/create_study_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/CreateGroupBtn"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="This is the page where you can create and view existing groups"
        android:id="@+id/textView"
        android:layout_below="@+id/create_study_group"/>

</RelativeLayout>

When the Button is clicked, the "Test" message is displayed but the app closes right after. Button被点击时,会显示“测试”的消息,但应用程序后立即关闭。

Here is the CreateGroup java class: 这是CreateGroup Java类:

public class CreateGroup extends Fragment{


    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.create_group, container, false);
        return rootview;
    }

}

and the create_group.xml file: create_group.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"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="This is the page where the form is displayed"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

NavigationActivity: NavigationActivity:

public class NavigationActivity extends FragmentActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    FragmentManager fragmentManager = getSupportFragmentManager();

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager =getSupportFragmentManager();
    Fragment fragment= new Fragment();

    switch (position){
        case 0:
            mTitle = getString(R.string.title_section1);
            fragment = new Menu1_Fragment();

            break;
        case 1:
            mTitle = getString(R.string.title_section2);
            fragment = new Menu2_Fragment();

            break;



    }
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment)
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.navigation, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    //if the log out button is selected, log out of Parse and go back to log in page
    if (id == R.id.action_logout) {
        ParseUser.logOut();
        Intent intent = new Intent(NavigationActivity.this,LoginSignupActivity.class);
        startActivity(intent);
        Toast.makeText(getApplicationContext(), "Successfully Logged out", Toast.LENGTH_LONG).show();

        finish();


    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((NavigationActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}

I'm having trouble figuring out why the CreateGroup class/layout file aren't being displayed as a new page. 我在弄清楚为什么CreateGroup类/布局文件未显示为新页面时遇到了麻烦。

CreateGroup类是一个片段,如果正在使用startActivity,则意图应使用活动

CreateGroup is a fragment, so you should change fragments and not start an activity: CreateGroup是一个片段,因此您应该更改片段而不是启动活动:

CreateGroupButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){       
    Toast.makeText(getActivity().getApplicationContext(), "Test", Toast.LENGTH_LONG).show();

    // Create new fragment and transaction
    Fragment createGroupFragment = new CreateGroup();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
        .replace(R.id.container, createGroupFragment)
        .commit();
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM