按钮在片段中单击时强制关闭

时间:2014-02-25 00:43:00

标签: android android-fragments android-xml

我有一个带有按钮的片段,当按下该按钮时,会从微调器和文本框中获取数据,然后启动电子邮件。如果将onClick代码放入片段或片段加载到的主要活动中,我有点困惑。

我尝试将我的onclick方法放入我的片段中并强制关闭,然后我将其移动到我的MainDrawer2活动中并且我关闭了这个力:

02-24 19:28:39.835  27674-27674/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3673)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3668)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.beerportfolio.beerportfoliopro.MainDrawer2.sendFeedback(MainDrawer2.java:216)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3668)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)

我的联系页面片段:

public class ContactPage extends Fragment {

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

        //set layout here
        View v = inflater.inflate(R.layout.activity_contact, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Contact Page");

        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        // Inflate the layout for this fragment
        return v;

    }


}

ContactPage xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">



                <!-- Card Contents go here -->
                <TextView
                    android:id="@+id/contactTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="Contact"
                    android:textSize="20sp"
                    android:textStyle = "bold"
                    android:padding="5dip"
                    >
                </TextView>



            </LinearLayout >

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_marginRight="6dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginBottom="4dp"
                    android:orientation="vertical"
                    android:background="@drawable/bg_card">



                    <!-- Card Contents go here -->


                    <TextView
                        android:id="@+id/subjectTitle"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:padding="5dip"
                        android:text="Subject:"
                        android:textSize="15sp"
                        android:textStyle="bold" >
                    </TextView>
                    <Spinner
                        android:id="@+id/contactSpinner"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:entries="@array/contact_array"

                        />



                </LinearLayout >

            </FrameLayout>

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">



                <!-- Card Contents go here -->



                <TextView
                    android:id="@+id/commentsTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:padding="5dip"
                    android:text="Comments:"
                    android:textSize="15sp"
                    android:textStyle="bold" >
                </TextView>
                <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:minLines="4"
                    android:maxLines="7"
                    android:ems="10"
                    android:id="@+id/editTextContact"
                    />


            </LinearLayout >

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:orientation="vertical"
                android:background="@drawable/bg_card">



                <!-- Card Contents go here -->
                <Button
                    android:id="@+id/buttonContact"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="5dip"
                    android:text="Submit Feedback"
                    android:onClick="sendFeedback"

                    />


            </LinearLayout >

        </FrameLayout>



    </LinearLayout>
</ScrollView>

MainDrawer2:

public class MainDrawer2 extends FragmentActivity
{
    private static final String EXTRA_NAV_ITEM    = "extraNavItem";
    private static final String STATE_CURRENT_NAV = "stateCurrentNav";

    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;

    private NavDrawerListAdapter mDrawerAdapter;
    private ListView mDrawerList;

    private CharSequence mTitle;
    private CharSequence mDrawerTitle;

    private MainNavItem mCurrentNavItem;


    public static Intent createLaunchFragmentIntent(Context context, MainNavItem navItem)
    {
        return new Intent(context, MainDrawer2.class)
                .putExtra(EXTRA_NAV_ITEM, navItem.ordinal());
    }

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

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList   = (ListView)findViewById(R.id.drawer);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        enableHomeButtonIfRequired();

        mDrawerAdapter = new NavDrawerListAdapter(getApplicationContext());
        mDrawerList.setAdapter(mDrawerAdapter);
        mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                displayNavFragment((MainNavItem)parent.getItemAtPosition(position));
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.app_name, R.string.app_name)
        {
            public void onDrawerClosed(View view)
            {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView)
            {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if(getIntent().hasExtra(EXTRA_NAV_ITEM)){
            MainNavItem navItem = MainNavItem.values()
                    [getIntent().getIntExtra(EXTRA_NAV_ITEM,
                    MainNavItem.STATISTICS.ordinal())];
            displayNavFragment(navItem);
        }
        else if(savedInstanceState != null){
            mCurrentNavItem = MainNavItem.values()
                    [savedInstanceState.getInt(STATE_CURRENT_NAV)];
            setCurrentNavItem(mCurrentNavItem);
        }
        else{
            displayNavFragment(MainNavItem.STATISTICS);
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void enableHomeButtonIfRequired()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            getActionBar().setHomeButtonEnabled(true);
        }
    }

    @Override
    public void setTitle(CharSequence title)
    {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_CURRENT_NAV, mCurrentNavItem.ordinal());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /*
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
    */

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(mDrawerToggle.onOptionsItemSelected(item)){
            return true;
        }
        switch(item.getItemId()){
            case R.id.menu_search2:
                displayNavFragment(MainNavItem.SEARCH);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void displayNavFragment(MainNavItem navItem)
    {
        if(navItem == mCurrentNavItem){
            return;
        }
        Fragment fragment = Fragment.instantiate(this,
                navItem.getFragClass().getName());
        if(fragment != null){
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.main, fragment)
                    .commit();
            setCurrentNavItem(navItem);
        }
    }

    private void setCurrentNavItem(MainNavItem navItem)
    {
        int position = navItem.ordinal();
        // If navItem is in DrawerAdapter
        if(position >= 0 && position < mDrawerAdapter.getCount()){
            mDrawerList.setItemChecked(position, true);
        }
        else{
            // navItem not in DrawerAdapter, de-select current item
            if(mCurrentNavItem != null){
                mDrawerList.setItemChecked(mCurrentNavItem.ordinal(), false);
            }
        }
        mDrawerLayout.closeDrawer(mDrawerList);
        setTitle(navItem.getTitleResId());
        mCurrentNavItem = navItem;
    }


    public void sendFeedback(View view){

        //get spinner content
        Spinner subjectSpinner = (Spinner) view.findViewById(R.id.contactSpinner);
        String subjectText = subjectSpinner.getSelectedItem().toString();

        //get feedback context
        EditText feedback   = (EditText)view.findViewById(R.id.editTextContact);
        String feedbackToSend = feedback.getText().toString();


        //launch gmail
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mike@beerportfolio.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, subjectText);
        i.putExtra(Intent.EXTRA_TEXT   , feedbackToSend);
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }



    }

}

更新

稍微更改了我的代码。这次我尝试在代码中设置一个onCLick监听器,而不是依赖于xml中的onClick。我片段的新代码是:

public class ContactPage extends Fragment {

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

        //set layout here
        View v = inflater.inflate(R.layout.activity_contact, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Contact Page");

        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        Button bt = (Button) v.findViewById(R.id.buttonContact);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Spinner subjectSpinner = (Spinner) v.findViewById(R.id.contactSpinner);

                String subjectText = subjectSpinner.getSelectedItem().toString();

                //get feedback context
                EditText feedback   = (EditText)v.findViewById(R.id.editTextContact);
                String feedbackToSend = feedback.getText().toString();


                //launch gmail
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mike@beerportfolio.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, subjectText);
                i.putExtra(Intent.EXTRA_TEXT   , feedbackToSend);
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(getActivity(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }




            }

        });


        // Inflate the layout for this fragment
        return v;

    }


}

我仍然在线上发出强制关闭错误:

String subjectText = subjectSpinner.getSelectedItem().toString();

力量关闭:

02-24 20:13:32.078  32759-32759/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.beerportfolio.beerportfoliopro.ContactPage$1.onClick(ContactPage.java:47)
            at android.view.View.performClick(View.java:4280)
            at android.view.View$PerformClick.run(View.java:17984)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5789)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

//你已经引用查看为v

//这是你的根视图

View v = inflater.inflate(R.layout.activity_contact, container, false);

//并点击按钮,即可传递View v

 public void onClick(View v) {

Spinner subjectSpinner = (Spinner) v.findViewById(R.id.contactSpinner);

<强>溶胶:

1)  Declare the Spinner outside the onClick()
2) change the `onClick(View btnView)`

答案 1 :(得分:0)

// Why do u assume spinner always have something selected

    spinner.setOnItemSelectedListener(this);

    public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {
    // This is the point where u assume u have an item selected, not before this
    // Here u will not get NPE
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }