我正在尝试在Fragment中创建一个EditText,一个Button和一个OnClickListener以与JavaMail一起使用,但是应用程序一直在崩溃,如果我删除了这个视图,那么应用程序工作正常,但JavaMail却没有。我不知道问题是在Fragment代码中还是在其他地方。我是编程新手,所以欢迎任何帮助。
FragmentOne.Java
package info.androidhive.materialtabs.fragments;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.SendMail;
public class OneFragment extends Fragment implements View.OnClickListener {
//Declaring EditText
private EditText editTextMessage;
//Send button
private Button buttonSend;
public OneFragment() {
} // Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
private void sendEmail() {
//Getting content for email
String email = "myemail@gmail.com";
String subject = "mySubject";
String message = editTextMessage.getText().toString().trim();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
@Override
public void onClick(View v) {
sendEmail();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
editTextMessage = (EditText) getView().findViewById(R.id.editTextMessage);
buttonSend = (Button) getView().findViewById(R.id.buttonSend);
//Adding click listener
buttonSend.setOnClickListener(this);
return rootView;
}
}
fragment_one.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="@string/chamado"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:textStyle="normal" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/preencha" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="@string/descreva_o_problema"
android:inputType="textMultiLine" />
<Button
android:id="@+id/buttonSend"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginTop="44dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text="@string/enviar"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
错误
11-22 16:43:00.148 30210-30210/? E/Zygote: MountEmulatedStorage()
11-22 16:43:00.148 30210-30210/? E/Zygote: v2
11-22 16:43:00.158 30210-30210/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-22 16:43:03.198 30210-30210/info.androidhive.materialtabs E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.androidhive.materialtabs, PID: 30210
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at info.androidhive.materialtabs.fragments.OneFragment.onCreateView(OneFragment.java:60)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:607)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:674)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
at android.view.View.measure(View.java:18425)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2881)
at android.view.View.measure(View.java:18425)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2237)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:1)
您的视图不会在片段的onCreate()
方法上膨胀。
使用适当的生命周期方法(例如onCreateView
)初始化您的小部件
例如:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(<your.custom.layout>, container, false);
editTextMessage = rootView.findViewById(R.id.editTextMessage);
buttonSend = rootView.findViewById(R.id.buttonSend);
//Adding click listener
buttonSend.setOnClickListener(this)
return rootView;
}
Here你可以理解片段生命周期。