在editext框中显示条形码

时间:2012-04-11 17:44:34

标签: android android-edittext zxing

这里我再次尝试修复我的一些代码,但仍然无法正常工作请帮助我。 我的问题是在显示器上,我真的不知道怎么做但我真的认为我很接近。这是我的代码:

public class Addform extends Activity {
    DBAdapter db;
    private static final int REQUEST_BARCODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addfom);

        db = new DBAdapter(this);

        Button Save = (Button)findViewById(R.id.save);
        Save.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View arg0)
            {
                {
                EditText edittype = (EditText) findViewById(R.id.edittype);
                EditText editweight = (EditText) findViewById(R.id.editweight);
                EditText editexp = (EditText) findViewById(R.id.editexp);
                if (edittype.getText().length() != 0 
                    && editweight.getText().length()  != 0
                    && editexp.getText().length()  != 0 
                    ) 
                    {
                        String type = edittype.getText().toString();
                        int weight = Integer.parseInt(editweight.getText().toString());
                        int exp = Integer.parseInt(editexp.getText().toString());
                        db.open();
                        long id = db.insertCar(type, weight, exp);        
                        db.close();
                        Toast.makeText(Addform.this,"Entry succesful",Toast.LENGTH_SHORT).show();
                        edittype.setText("");
                        editweight.setText("");
                        editexp.setText("");
                    }
                    else 
                    {
                        Toast.makeText(Addform.this,"Error: fields cannot be left empty",Toast.LENGTH_LONG).show();     
                    }
                }
            }
        });
        Button Scan = (Button)findViewById(R.id.scan);
        Scan.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                EditText mBarcodeEdit = (EditText) findViewById(R.id.barcode);
                switch (v.getId()) {
                case R.id.scan:
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
                    startActivityForResult(intent, REQUEST_BARCODE);
                    break;
                }
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_BARCODE) {
            if (resultCode == RESULT_OK) {
                String barcodetext = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                EditText mBarcodeEdit = (EditText) findViewById(R.id.barcode);
                mBarcodeEdit.setText(barcodetext);
                // Handle successful scan
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                finish();
            }
        }
    }
}

这是.XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">

<TextView
    android:id="@+id/type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type" />

<EditText
    android:id="@+id/edittype"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Weight" />

<EditText
    android:id="@+id/editweight"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/exp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Expiration date" />

<EditText
    android:id="@+id/editexp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="date" />

<TextView
    android:id="@+id/barcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Barcode" />

<EditText
    android:id="@+id/editbarcode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number" />

<Button
    android:id="@+id/scan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Scan" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:weightSum="1">

    <Button
        android:id="@+id/save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Save" />

</LinearLayout>

我尝试了

EditText mBarcodeEdit =(EditText)findViewById(R.id.barcode); 到处都是,但它似乎没有认出来,当我把它放在“onActivityResult”方法时,它会崩溃应用程序。 请帮帮我

这是日志

04-11 19:04:22.980: W/IInputConnectionWrapper(27290): showStatusIcon on inactive InputConnection
04-11 19:04:32.505: D/AndroidRuntime(27290): Shutting down VM
04-11 19:04:32.505: W/dalvikvm(27290): threadid=1: thread exiting with uncaught exception (group=0x40a461f8)
04-11 19:04:32.505: E/AndroidRuntime(27290): FATAL EXCEPTION: main
04-11 19:04:32.505: E/AndroidRuntime(27290): java.lang.RuntimeException: Failure
delivering result ResultInfo{who=null, request=0, result=-1, data=Intent {
act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity
{wijayaratnam.sutharsun.mobilefridge/wijayaratnam.sutharsun.mobilefridge.Addform}:
java.lang.ClassCastException: android.widget.TextView cannot be cast to
android.widget.EditText
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.ActivityThread.access$1100(ActivityThread.java:123)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.os.Handler.dispatchMessage(Handler.java:99)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.os.Looper.loop(Looper.java:137)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.ActivityThread.main(ActivityThread.java:4424)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
java.lang.reflect.Method.invokeNative(Native Method)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
java.lang.reflect.Method.invoke(Method.java:511)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
dalvik.system.NativeStart.main(Native Method)
04-11 19:04:32.505: E/AndroidRuntime(27290): Caused by: java.lang.ClassCastException:
android.widget.TextView cannot be cast to android.widget.EditText
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
wijayaratnam.sutharsun.mobilefridge.Addform.onActivityResult(Addform.java:214)
04-11 19:04:32.505: E/AndroidRuntime(27290):    at
android.app.Activity.dispatchActivityResult(Activity.java:4649)
04-11 19:04:32.505: E/AndroidRuntime(27290): at
app.ActivityThread.deliverResults(ActivityThread.java:2976)
04-11 19:04:32.505: E/AndroidRuntime(27290):    ... 11 more
04-11 19:04:32.925: I/dalvikvm(27290): threadid=3: reacting to signal 3
04-11 19:04:32.940: I/dalvikvm(27290): Wrote stack traces to '/data/anr/traces.txt'
04-11 19:04:33.035: I/dalvikvm(27290): threadid=3: reacting to signal 3
04-11 19:04:33.040: I/dalvikvm(27290): Wrote stack traces to '/data/anr/traces.txt'

2 个答案:

答案 0 :(得分:1)

在您看来,您声明了这一点:

<TextView
    android:id="@+id/barcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Barcode" />

然后,在你的代码中,你试试这个:

EditText mBarcodeEdit = (EditText) findViewById(R.id.barcode);
你看到了吗?您将@ + id /条形码声明为TextView,然后尝试将其引用为EditText

因此,要么将布局更改为

<EditText
    android:id="@+id/barcode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Barcode" />

将您的代码更改为:

TextView mBarcodeEdit = (TextView) findViewById(R.id.barcode);

答案 1 :(得分:0)

EditText mBarcodeEdit = (EditText) findViewById(R.id.barcode);

用以下

更改以上行
EditText mBarcodeEdit = (EditText) findViewById(R.id.editbarcode);