在这里,我再次尝试修复我的一些代码,但仍然无法正常工作,请帮助我。我的问题出在显示器上,我真的不知道该怎么做,但我真的认为我已经接近了。这是我的代码:
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'