0

Trying to make a button when press would open up e-mail. Sorry for being a noob only beginner. Any help would be awesome, thank you Crashes when I click on fragment not sure whats wrong, again any help is much appreciated

Heres code:

 public class Fragment_7 extends Fragment{
private Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_7,
            container, false);

    final Context context = getActivity();

    Button button = (Button) getView().findViewById(R.id.button);

            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    sendEmail(context, new String[]{"info@bottlecapp.com"}, "Sending Email", "App Development", "");
                }
            });
            return view;
            }

 public static void sendEmail(Context context, String[] recipientList,
        String title, String subject, String body) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    emailIntent.setType("plain/text");

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipientList);

    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

    context.startActivity(Intent.createChooser(emailIntent, title));

     }
 }





 06-15 18:28:56.719: E/AndroidRuntime(553): FATAL EXCEPTION: main
 06-15 18:28:56.719: E/AndroidRuntime(553): java.lang.NullPointerException
 06-15 18:28:56.719: E/AndroidRuntime(553):     at      com.bottlecapp.bottlecapp.Fragment_7.onCreateView(Fragment_7.java:24)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.os.Handler.handleCallback(Handler.java:605)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.os.Handler.dispatchMessage(Handler.java:92)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.os.Looper.loop(Looper.java:137)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at android.app.ActivityThread.main(ActivityThread.java:4424)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at java.lang.reflect.Method.invoke(Method.java:511)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 06-15 18:28:56.719: E/AndroidRuntime(553):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

改变final Context context = Fragment_7.this;

final Context context = getActivity();

于 2013-06-15T18:07:45.280 回答
1

由于 getView 返回您在 onCreateView 中膨胀的内容,因此在 onCreateView 中调用它将返回 null。该方法还必须返回一个视图对象。以这种方式更改它:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_7,
        container, false);

final Context context = getActivity();

Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                sendEmail(context, new String[]{",com"}, "Sending Email", "", "");
            }
        });
    return view;

}
于 2013-06-15T18:09:48.983 回答