按下时尝试按下按钮会打开电子邮件。抱歉,只有初学者才是新手。任何帮助都会很棒,谢谢 当我点击片段不确定什么是错的时候崩溃,再次感谢任何帮助
Heres代码:
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)
答案 0 :(得分:1)
更改final Context context = Fragment_7.this;
final Context context = getActivity();
答案 1 :(得分: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;
}