将internal:spinner值和textview存储为整数

时间:2014-02-11 17:42:28

标签: java android xml crash integer

现在谷歌搜索了2天,我真的无法解决这个问题。 无论如何,我使用的方法适用于EditTexts,但不适用于TextViews和Spinners。按下保护程序按钮时会崩溃

我是初学者,所以详细的解释是适用的!谢谢!

Java保存部分:

public void saver(View view) throws Exception

  { 
    String mag = readFields();
      saveToFile(mag);
  }

  private String readFields()
  { 
      int[] inputIDs = new int[]{
                                R.id.email_address,
                                R.id.text_date,
                                R.id.txtTime,
                                R.id.spinner1,
                                R.id.spinner2
                                };
     String outputMag = "";
     for( int id: inputIDs)
     {

         EditText textField = (EditText)findViewById(id);
         outputMag +=  textField.getText().toString() + ",";
     }
     return outputMag.substring(0, outputMag.length() -1 ) + "\n";
  }

  private void saveToFile(String message) throws Exception
  {
      String filePath = getFilesDir().toString() + "/contact.txt";
      FileOutputStream out = new FileOutputStream(filePath, true);
      out.write(message.getBytes());
      out.close();
  }

整个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=".AddEventActivity" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/txtTime"
        android:text="@string/ch_label2" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignTop="@+id/text_date"
        android:text="@string/ch_label" />

    <EditText
        android:id="@+id/email_address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/addeventname"
        android:inputType="text" />

    <TextView
        android:id="@+id/text_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/email_address"
        android:layout_marginTop="20dp"
        android:text="text_date txt"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button"
        android:layout_marginTop="18dp"
        android:text="text_time txt"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:entries="@array/country_array"
        android:prompt="@string/select" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1"
        android:entries="@array/upprepa_array"
        android:prompt="@string/select2" />

    <Button
        android:id="@+id/buttontospinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner2"
        android:text="@string/button_label" 
        android:onClick="saver" />

崩溃时的LogCat:

02-11 18:31:12.058: D/dalvikvm(7771): GC_CONCURRENT freed 67K, 2% free 9444K/9607K, paused 9ms+25ms
02-11 18:31:12.338: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:12.478: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:22.419: D/AndroidRuntime(7771): Shutting down VM
02-11 18:31:22.419: W/dalvikvm(7771): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-11 18:31:22.459: E/AndroidRuntime(7771): FATAL EXCEPTION: main
02-11 18:31:22.459: E/AndroidRuntime(7771): java.lang.IllegalStateException: Could not execute method of the activity
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.view.View$1.onClick(View.java:3044)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.view.View.performClick(View.java:3511)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.view.View$PerformClick.run(View.java:14105)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.os.Handler.handleCallback(Handler.java:605)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.os.Looper.loop(Looper.java:137)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at java.lang.reflect.Method.invoke(Method.java:511)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at dalvik.system.NativeStart.main(Native Method)
02-11 18:31:22.459: E/AndroidRuntime(7771): Caused by: java.lang.reflect.InvocationTargetException
02-11 18:31:22.459: E/AndroidRuntime(7771):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at java.lang.reflect.Method.invoke(Method.java:511)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at android.view.View$1.onClick(View.java:3039)
02-11 18:31:22.459: E/AndroidRuntime(7771):     ... 11 more
02-11 18:31:22.459: E/AndroidRuntime(7771): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
02-11 18:31:22.459: E/AndroidRuntime(7771):     at apps.rasmusmyhrberg.smartcalendar.AddEventActivity.readFields(AddEventActivity.java:67)
02-11 18:31:22.459: E/AndroidRuntime(7771):     at apps.rasmusmyhrberg.smartcalendar.AddEventActivity.saver(AddEventActivity.java:50)
02-11 18:31:22.459: E/AndroidRuntime(7771):     ... 14 more
02-11 18:31:23.099: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:23.119: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:23.669: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:23.669: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:24.159: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:24.179: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:24.719: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:24.739: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:25.290: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:25.309: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:25.859: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:25.879: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:26.489: I/dalvikvm(7771): threadid=3: reacting to signal 3
02-11 18:31:26.489: I/dalvikvm(7771): Wrote stack traces to '/data/anr/traces.txt'
02-11 18:31:26.549: I/Process(7771): Sending signal. PID: 7771 SIG: 9

1 个答案:

答案 0 :(得分:0)

您正在将每个元素投射到EditText。当其中一个inputIDs未引用EditText时,该元素将是另一个类。 TextViewSpinner s不是EditText的实例,因此无法转换为EditText

您需要查看您关注的每种类型,并调用相应的方法来检索所需的信息。然后,您可以将它们组合成输出消息。