2014-09-23 2 views
0

Я использую google currency api для конвертации валют. Но я встречаю странное исключение класса и не могу понять, что происходит.Strange ClassCastException: android.widget.textview

Я отправляю весь XML:

<include 
    android:id="@+id/footer" 
    layout="@layout/footer" /> 

<include 
    android:id="@+id/header" 
    layout="@layout/header" /> 

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/footer" 
    android:layout_below="@id/header" > 

    <RelativeLayout 
     android:id="@+id/rlbody" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="18dp" 
     android:background="#ffffff" > 

     <RelativeLayout 
      android:id="@+id/rlfrom" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" > 

      <Spinner 
       android:id="@+id/from_currency" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txtfromtext" 
       android:background="@drawable/spinner_bg" /> 

     <TextView 
       android:id="@+id/txtfromtext" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:text="From Currency:" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/rlto" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/rlfrom" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/txttotext" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:text="To Currency:" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <Spinner 
       android:id="@+id/to_currency" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txttotext" 
       android:background="@drawable/spinner_bg" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/rlexchangeamt" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/rlto" 
      android:layout_marginTop="20dp" > 

      <TextView 
       android:id="@+id/txtamt" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:text="Exchange Amount:" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <EditText 
       android:id="@+id/input_amount" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/txtamt" 
       android:layout_margin="3dp" 
       android:background="@drawable/editbackground" 
       android:gravity="center" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:id="@+id/lv_button" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/rlexchangeamt" 
      android:orientation="horizontal" 
      android:weightSum="1" > 

      <Button 
       android:id="@+id/convert" 
       android:layout_width="30dp" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:layout_weight="0.5" 
       android:background="#9d1515" 
       android:padding="12dp" 
       android:text="Calculate" 
       android:textColor="#ffffff" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <Button 
       android:id="@+id/clear_input" 
       android:layout_width="30dp" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:layout_weight="0.5" 
       android:background="#181818" 
       android:padding="12dp" 
       android:text="Reset" 
       android:textColor="#ffffff" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/output_amount" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/lv_button" 
      android:layout_marginTop="10dp" 
      android:gravity="center" 
      android:paddingBottom="5dp" 
      android:paddingRight="5dp" 
      android:paddingTop="5dp" 
      android:text="hello" 
      android:textColor="#9d1515" 
      android:textSize="20sp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 
</ScrollView> 

Ирония:classcast исключение показывает только после того, как я вставил Scrollview .Before что все было в порядке.

Соответствующий код:

public class CurrencyConverterActivity extends Activity implements 
OnClickListener, OnItemSelectedListener, TextWatcher { 
    private static final String TAG = "UnitConverterActivity"; 
    TextView tvheader; 
    private Spinner fromCurrency; 
    private Spinner toCurrency; 
    private EditText inputAmount; 
    private TextView outputAmount, tv_header; 
    private View convert; 
    private View reverseCurrencies; 
    private View clearInput; 
    private CurrencyConverter currencyConverter; 
    Activity mBaseActivity; 
    String namearray[]; 
    List<Currency> curr ; 
    String fromcode,tocode; 
    ImageButton btnback; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.currency_converter); 
     btnback=(ImageButton)findViewById(R.id.btn_back); 
     btnback.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent i=new Intent(CurrencyConverterActivity.this,MainActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     }); 
     tvheader=(TextView)findViewById(R.id.tv_pagetitle); 
     tvheader.setText("Currency Converter"); 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(),  R.drawable.pattern_bg); 
     BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); 
     bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 
     RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl_main); 
     rl.setBackgroundDrawable(bitmapDrawable); 
      curr = new ArrayList<Currency>(); 
    //A  
      curr.add(new Currency("United Arab Emirates Dirham(AED)","AED")); 
      curr.add(new Currency("Afghan Afghani(AFN)","AFN")); 
      curr.add(new Currency("Albanian Lek(ALL)","ALL")); 
      curr.add(new Currency("Armenia Dram(AMD)","AMD")); 
      curr.add(new Currency("Netherlands Antilles Guilder(ANG)","ANG")); 
      curr.add(new Currency("Angola Kwanza(AOA)","AOA")); 
      curr.add(new Currency("Argentina Peso(ARS)","ARS")); 
      curr.add(new Currency("Australia Dollar(AUD)","AUD")); 
      curr.add(new Currency("Aruba Guilder(AWG)","AWG")); 





      namearray=new String[curr.size()]; 
      for(int i=0;i<curr.size();i++) 
      { 
      Currency item = curr.get(i); 
      namearray[i]=item.getName(); 
      } 

      ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,namearray); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 



     this.fromCurrency = (Spinner) super.findViewById(R.id.from_currency); 
     this.fromCurrency.setOnItemSelectedListener(this); 
     fromCurrency.setAdapter(adapter); 



     this.toCurrency = (Spinner) super.findViewById(R.id.to_currency); 
     this.toCurrency.setOnItemSelectedListener(this); 
     toCurrency.setAdapter(adapter); 


     this.inputAmount = (EditText) super.findViewById(R.id.input_amount); 
     this.inputAmount.setText("1"); 
     this.inputAmount.addTextChangedListener(this); 

     this.outputAmount = (TextView) super.findViewById(R.id.output_amount); 

     this.convert = super.findViewById(R.id.convert); 
     this.convert.setOnClickListener(this); 


     this.clearInput = super.findViewById(R.id.clear_input); 
     this.clearInput.setOnClickListener(this); 


     this.currencyConverter = new GoogleCurrencyConverter(); 
    } 

LogCat:

09-23 10:43:50.876: E/AndroidRuntime(1693): FATAL EXCEPTION: main 
09-23 10:43:50.876: E/AndroidRuntime(1693): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webguru.calculatorapp/com.webguru.calculatorapp.CurrencyConverterActivity}: java.lang.ClassCastException: android.widget.TextView 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1740) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1759) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread.access$1500(ActivityThread.java:155) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.os.Handler.dispatchMessage(Handler.java:130) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.os.Looper.loop(SourceFile:351) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread.main(ActivityThread.java:3826) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at java.lang.reflect.Method.invoke(Method.java:538) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at dalvik.system.NativeStart.main(Native Method) 
09-23 10:43:50.876: E/AndroidRuntime(1693): Caused by: java.lang.ClassCastException: android.widget.TextView 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at com.webguru.calculatorapp.CurrencyConverterActivity.onCreate(CurrencyConverterActivity.java:373) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1704) 
09-23 10:43:50.876: E/AndroidRuntime(1693):  ... 11 more 

Порядковый номер 373: this.toCurrency = (Spinner) super.findViewById (R.id.to_currency);

У меня нет абсолютно никакого представления о том, что я здесь делаю. Помочь.

+0

да, посмотрите на код ... Я сделал то, что вы сказали. – kgandroid

+0

Вы называете правильный XML? Попробуйте следовать символу 'R.id.to_currency'. Возможно, он импортируется из другого XML. – zozelfelfo

+0

, конечно, я ........... – kgandroid

ответ

0

не удался найти tv_pagetitle. Посмотрите, есть ли текстовое представление с именем tv_pagetitle в вашем xml.

+0

Спасибо, что указали, что out.Actually у меня есть 2 заголовка Один для панели инструментов и другие для остальных layouts.and в обоих заголовках Я использовал tv_pagetitle и, следовательно, конфликт. Но до сих пор я не понимаю, почему это даст исключение в коде spinner. – kgandroid

0

Попробуйте изменить super.findViewById на номер findViewById, который должен это сделать.

Надеется, что это помогает :)

+1

Выполнено, что уже.тоже результат – kgandroid

0

попробовать это меняет,

fromCurrency = (Spinner) findViewById(R.id.from_currency); 

я не могу найти tv_pagetitle в вашем XML-лист, пожалуйста, проверьте его.

tvheader=(TextView)findViewById(R.id.tv_pagetitle); 
0

Вы не упомянули этот идентификатор текста в своем XML-файле.

tvheader=(TextView)findViewById(R.id.tv_pagetitle); 

лучше изменить

this.outputAmount = (TextView) super.findViewById(R.id.output_amount) 

Для

outputAmount = (TextView)findViewById(R.id.output_amount) 
Смежные вопросы