gpt4 book ai didi

java - EditText 上的 NullPointerException .getText().toString()

转载 作者:行者123 更新时间:2023-12-01 17:17:09 26 4
gpt4 key购买 nike

我在“textTo.getText().toString();”上收到 nullPointerException以下。我不明白为什么。我也尝试过将 EditText 的 findViewById 放在 onClick 中,但没有区别。

Maila.Java:

public class Maila extends Fragment implements OnClickListener{

public static Maila newInstance() {
Maila f = new Maila();
System.out.println("Initialiserad");
return f;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.contact_form, container, false);

return v;
}

Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessage;
EditText textName;
EditText textTel;
EditText textFrom;

@Override
public void onViewCreated (View v, Bundle SavedInstanceState){
buttonSend = (Button) getView().findViewById(R.id.mailKnapp);
textTo = (EditText) getView().findViewById(R.string.mailAdress);
textSubject = (EditText) getView().findViewById(R.id.editTextMeddelandeTitel);
textName = (EditText) getView().findViewById(R.id.editTextNamn);
textTel = (EditText) getView().findViewById(R.id.editTextTel);
textMessage = (EditText) getView().findViewById(R.id.editTextMeddelande);
textFrom = (EditText) getView().findViewById(R.id.editTextEpost);
Button mailKnapp = (Button)v.findViewById(R.id.mailKnapp);
mailKnapp.setOnClickListener(this);

}



@Override
public void onClick(View v) {
String to = textTo.getText().toString();
System.out.println("textTo: " + to);
String subject = textSubject.getText().toString();
System.out.println("textSubject: " + subject);
String message = textMessage.getText().toString();
System.out.println("textMessage: " + message);
String namn = textName.getText().toString();
System.out.println("textName: " + namn);
String telefon = textTel.getText().toString();
System.out.println("textTel: " + telefon);

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
//email.putExtra(Intent.EXTRA_CC, new String[]{ to});
//email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, ("Skickat från TestApp FöretagsInfo: " + subject));
email.putExtra(Intent.EXTRA_TEXT, ("Från: " + namn + ", " + textFrom + "\nTelefon: " + telefon + "\nMeddelande:\n" + message));

//need this to prompts email client only
email.setType("message/rfc822");



try {
startActivity(Intent.createChooser(email, "Skicka med: "));
} catch (android.content.ActivityNotFoundException exception) {
Toast.makeText(Maila.this.getActivity(), "Ingen E- postklient kunde hittas på enheten!!", Toast.LENGTH_LONG).show();
}
}
}

日志猫:

    01-17 21:58:26.576: D/AndroidRuntime(21589): Shutting down VM
01-17 21:58:26.581: W/dalvikvm(21589): threadid=1: thread exiting with uncaught exception (group=0x413462a0)
01-17 21:58:26.606: E/AndroidRuntime(21589): FATAL EXCEPTION: main
01-17 21:58:26.606: E/AndroidRuntime(21589): java.lang.NullPointerException
01-17 21:58:26.606: E/AndroidRuntime(21589): at nu.sluggo.testapp.foretagsinfo.Maila.onClick(Maila.java:64)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.view.View.performClick(View.java:4232)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.view.View$PerformClick.run(View.java:17298)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.os.Handler.handleCallback(Handler.java:615)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.os.Looper.loop(Looper.java:137)
01-17 21:58:26.606: E/AndroidRuntime(21589): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-17 21:58:26.606: E/AndroidRuntime(21589): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:58:26.606: E/AndroidRuntime(21589): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 21:58:26.606: E/AndroidRuntime(21589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-17 21:58:26.606: E/AndroidRuntime(21589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
01-17 21:58:26.606: E/AndroidRuntime(21589): at dalvik.system.NativeStart.main(Native Method)
01-17 22:03:35.716: I/Process(21589): Sending signal. PID: 21589 SIG: 9

contact_form.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"
android:background="@color/BGColor" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/BGColor" >

<TextView
android:id="@+id/mailTitel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mailTitel"
android:textSize="@dimen/mailTitel" />

<TextView
android:id="@+id/mailVarning"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mailVarning"
android:textSize="@dimen/mailVarning" />

<TextView
android:id="@+id/mailInfo"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mailInfo"
android:textColor="@color/mailInfo"
android:textSize="@dimen/mailInfo" />

<EditText
android:id="@+id/editTextNamn"
android:layout_marginTop="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/boxboarder"
android:ems="10"
android:hint="@string/editTextNamn"
android:textColorHint="@color/editTextHint"
android:textSize="@dimen/editText_textSize"
android:singleLine="true"
android:inputType="textCapWords" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/editTextTel"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/boxboarder"
android:ems="10"
android:inputType="phone"
android:hint="@string/editTextTel"
android:textColorHint="@color/editTextHint"
android:textSize="@dimen/editText_textSize"
android:singleLine="true" />

<EditText
android:id="@+id/editTextEpost"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/boxboarder"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="@string/editTextEpost"
android:textColorHint="@color/editTextHint"
android:textSize="@dimen/editText_textSize"
android:singleLine="true" />

<EditText
android:id="@+id/editTextMeddelandeTitel"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/boxboarder"
android:ems="10"
android:hint="@string/editTextMeddelandeTitel"
android:textColorHint="@color/editTextHint"
android:textSize="@dimen/editText_textSize"
android:singleLine="true"
android:inputType="textCapSentences" />

<EditText
android:id="@+id/editTextMeddelande"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/boxboarder"
android:ems="10"
android:inputType="textMultiLine|textCapSentences"
android:hint="@string/editTextMeddelande"
android:textColorHint="@color/editTextHint"
android:textSize="@dimen/editText_textSize"
android:minLines="4" />

<Button
android:id="@+id/mailKnapp"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="skickaMail"
android:text="@string/skickaKnapp" />

</LinearLayout>
</ScrollView>

(字符串.XML:)

...
<string name="mailAdress">test@sluggotech.se</string>
...

最佳答案

因为:

textTo = (EditText) getView().findViewById(R.string.mailAdress);

您没有该名称的 EditText,因此它返回 null

怀疑您打算使用R.id.mailInfo

编辑添加:这里似乎存在多个问题,这就是一个。

另一个原因是您没有使用传递给 onViewCreated()View。所有这些调用应该是:

v.findViewById(...);

但这不太可能起作用,因为我在任何地方都没有看到名为 contact_form 的布局。

关于java - EditText 上的 NullPointerException .getText().toString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21195875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com