我试图遍历EditText id的数组以获取所有文本并将它们添加到单个邮件中。我理解NullPointers是如何工作的,但是如果我像以前一样初始化它们,我就无法理解temp和temp2是如何NullPointers的,只是这次它处于循环中。这是我得到的错误:
Process: com.example.brandon.netflixcalculator, PID: 6369
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.brandon.netflixcalculator.Exact_Activity.getInfo(Exact_Activity.java:152)
at com.example.brandon.netflixcalculator.Exact_Activity.updateFile(Exact_Activity.java:125)
at com.example.brandon.netflixcalculator.Exact_Activity$Listener.onClick(Exact_Activity.java:76)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
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:6895)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
以下是方法:
int[] editTextIds = {R.id.editTextMon, R.id.editTextTues, R.id.editTextWed, R.id.editTextThurs, R.id.editTextFri,
R.id.editTextSat, R.id.editTextSun};
int[] spinnerIds = {R.id.spinnerMon, R.id.spinnerTues, R.id.spinnerWed, R.id.spinnerThurs, R.id.spinnerFri,
R.id.spinnerSat, R.id.spinnerSun};
public String getInfo(){
String msg = "";
StringBuffer sb = new StringBuffer();
for(int i = 0; i < editTextIds.length; i++){
EditText temp = (EditText)findViewById(editTextIds[i]);
Spinner temp2 = (Spinner)findViewById(spinnerIds[i]);
sb.append(temp.getText().toString());
sb.append(".");
sb.append(temp2.getSelectedItem().toString());
sb.append("\n");
}
msg = sb.toString();
return msg;
}
这是我的布局。一周中的每一天都是它自己的片段,所以这里是星期一(除了每个组件的id之外它们都是一样的):
<FrameLayout 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="com.example.brandon.netflixcalculator.Monday"
style="@style/Main">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextMon"
android:layout_gravity="center_horizontal"
android:background="@drawable/apptheme_edit_text_holo_light"
android:hint="Average number of available viewing minutes on Monday"
android:numeric="integer"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Probability of above viewing time on Monday"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textAlignment="center"
android:layout_marginTop="15dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerMon"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
</LinearLayout>