我对这个问题感到失望.我是android开发的新手.
我想使用ZXING的扫描结果.我将ZXING集成到我的Android应用程序中,扫描工作正常.现在我想使用扫描结果更新主活动中的TextView.
我怎样才能做到这一点?
我的代码现在正在执行以下操作:
>使用按钮打开主要活动.
>单击按钮时,它会激活扫描仪.
>扫描后,它将返回主要活动.
> TextView是空的,由于某种原因,它从横向移动到纵向后返回到原始内容.
在(2)之后我想使用扫描结果更新主要活动中的TextView(我的代码中的tv).
请帮帮我们
谢谢.
我的代码:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button pressToScan = (Button) findViewById(R.id.button1);
pressToScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent("com.google.zxing.client.android.SCAN");
data.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(data, 0);
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(data.getStringExtra("SCAN_RESULT"));
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String contents = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
解决方法:
移动以下块
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(data.getStringExtra("SCAN_RESULT"));
至
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String contents = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
//moved here
TextView tv = (TextView) findViewById(R.id.scanResult);
tv.setText(contents);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
[编辑]
在您的活动中添加此内容
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
在清单中更改主要活动以添加android:configChanges =“orientation”
<activity android:name="..."
android:label="@string/appName"
android:configChanges="orientation"