You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android货币转换应用Eclipse报错:字段未解析+XML格式问题求修复

Fixing Your Android Currency Converter App Issues in Eclipse

Let’s tackle both of your problems one by one—these are common pitfalls when starting out with Android development in Eclipse, so don’t stress!

1. Java Field Resolution Errors (convert, dollar, dtor, etc.)

What’s Causing It

The core issue here is that you’re importing Android’s system R class instead of your app’s own generated R file. The android.R only contains built-in Android resources, not the custom IDs you defined in main.xml (like @+id/dollar or @+id/convert). That’s why Eclipse can’t resolve those fields.

How to Fix It

  • Delete this line from your Java code:
    import android.R;
    
  • Eclipse should automatically import your app’s R class once it’s generated correctly. If it doesn’t, add this line manually (match it to your package name):
    import tugas.feb.R;
    
  • Note: Fixing your XML errors first will help Eclipse generate the R.java file properly, which might resolve this issue automatically.

Here’s the corrected Java code snippet:

package tugas.feb;

// Use your app's R class instead of the system one
import tugas.feb.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class TugasfebActivity extends Activity implements OnClickListener {
 TextView dollars;
 TextView rupiah;
 RadioButton dtor;
 RadioButton rtod;
 Button convert;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.main);
 dollars = (TextView)this.findViewById(R.id.dollar);
 rupiah = (TextView)this.findViewById(R.id.rupiah);
 dtor = (RadioButton)this.findViewById(R.id.dtor);
 rtod = (RadioButton)this.findViewById(R.id.rtod);
 convert = (Button)this.findViewById(R.id.convert);
 convert.setOnClickListener(this);
 }

 public void onClick(View v){
 if(dtor.isChecked()){
 convertDollarsToRupiah();
 }
 if(rtod.isChecked()){
 convertRupiahToDollars();
 }
 }

 protected void convertDollarsToRupiah(){
 double val = Double.parseDouble(dollars.getText().toString());
 rupiah.setText(Double.toString(val*12000));
 }

 protected void convertRupiahToDollars(){
 double val = Double.parseDouble(rupiah.getText().toString());
 dollars.setText(Double.toString(val/12000));
 }
}

2. XML Formatting Error (Line 11)

What’s Causing It

Your main.xml has three critical issues:

  1. Root element closed too early: You closed the RelativeLayout right after opening it, but all other views (Button, LinearLayouts) are placed outside the root—Android layouts require exactly one root element wrapping all other views.
  2. Attribute typos: Misspellings like adroid: (missing an 'n') and amdroid: (missing an 'n') break XML syntax.
  3. Duplicate attribute: The RadioGroup has two android:layout_width declarations.

How to Fix It

  • Move all views inside the root RelativeLayout (move the closing </RelativeLayout> to the end of the file).
  • Fix the typos (adroid: → android:, amdroid: → android:).
  • Remove the duplicate android:layout_width from the RadioGroup.

Here’s the corrected XML code:

<RelativeLayout android:id="@+id/widget33" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#F0E68C" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button android:id="@+id/convert" 
        android:layout_width="313px" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/widget38" 
        android:layout_centerHorizontal="true" 
        android:text="Convert" 
        android:textStyle="bold" />

    <LinearLayout android:id="@+id/widget38" 
        android:layout_width="320px" 
        android:layout_height="217px" 
        android:orientation="vertical" 
        android:layout_alignParentTop="true" 
        android:layout_alignParentLeft="true">

        <LinearLayout android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">

            <TextView android:id="@+id/widget39" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="Dollar" 
                android:textStyle="bold"/>

            <EditText android:id="@+id/dollar" 
                android:layout_width="123px" 
                android:layout_height="wrap_content" 
                android:textSize="22sp"/>
        </LinearLayout>

        <LinearLayout android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">

            <TextView android:id="@+id/widget40" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="Rupiah" 
                android:textStyle="bold"/>

            <EditText android:id="@+id/rupiah" 
                android:layout_width="127px" 
                android:layout_height="wrap_content" 
                android:textSize="22sp"/>
        </LinearLayout>

        <RadioGroup android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="vertical">

            <RadioButton android:id="@+id/dtor" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="Dollar ke Rupiah"/>

            <RadioButton android:id="@+id/rtod" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="Rupiah ke Dollar"/>
        </RadioGroup>
    </LinearLayout>
</RelativeLayout>

Final Steps

  1. Clean your Eclipse project: Go to Project > Clean... and select your project. This ensures Eclipse regenerates the R.java file correctly.
  2. Rebuild the project, and all errors should disappear.

内容的提问来源于stack exchange,提问作者Shanon.Saga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:14:43