我使用谷歌貨幣API來轉換currency.But我遇到了一個奇怪的類轉換異常,並不能找出發生了什麼。奇怪的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例外只顯示我已插入了滾動。之前之後每件事情都很好。
相關的代碼:
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 =(微調)super.findViewById(R.id.to_currency);
我完全不知道我在這裏做什麼錯。幫助。
是的,看看代碼...我已經做了你所說的話。 – kgandroid 2014-09-23 05:29:54
你在調用正確的XML嗎?嘗試使用符號'R.id.to_currency'。也許它是從另一個XML導入的。 – zozelfelfo 2014-09-23 05:33:42
當然,我是........... – kgandroid 2014-09-23 05:34:55