EditText .getText()。toString()上的NullPointerException

时间:2014-01-17 21:06:24

标签: java android nullpointerexception android-edittext

我在“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();
            }
}
}

logcat的:

    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>

(strings.xml中:)

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

2 个答案:

答案 0 :(得分:5)

由于:

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

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

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

编辑添加:此处出现多个问题,即一个问题。

另一个原因是你没有使用传递给View的{​​{1}}。所有这些电话应该是:

onViewCreated()

但这不太可行,因为我看不到名为v.findViewById(...); 的布局。

答案 1 :(得分:0)

在OnCreateView方法内部使v全局变量如

         View v=null;

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

                 return v;
             }

然后使用此v而不是getView

         buttonSend = (Button)v.findViewById(R.id.mailKnapp);