我在mainActivity中收到意图后尝试更改布局时遇到了问题。我有两个班级
MainActivity
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
MainActivity.context = getApplicationContext();
//two Intents are received
Intent i = this.getIntent();
Bundle extras = i.getExtras();
if(extras != null){
if(extras.containsKey("message"))
{ //another intent is received (works fine)}
if(extras.containsKey("scanContent")) {
Log.e(TAG, "scancontent intent received");
String msg = extras.getString("scanContent");
Log.e(TAG, msg);
arrowS.setVisibility(View.VISIBLE);//app is refreshed, but nothing is changed
}
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent scan = new Intent(MainActivity.this, Scan.class);
startActivity(scan);
} } });}
扫描课程:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//get the extras that are returned from the intent
String contents = intent.getStringExtra("SCAN_RESULT");
Intent i = new Intent(Scan.this, MainActivity.class);
i.putExtra("scanContent", contents);
startActivity(i);
}
}
}
@Override
public void onBackPressed() {
finish();
Intent intent = new Intent(Scan.this, MainActivity.class);
startActivity(intent);}
一切都有效,除了" arrowS.setVisibility(View.VISIBLE)"主要活动线
我没有在Logcat中收到任何错误消息,但我的Logcat输出/应用程序已更新,并且没有任何更改。有谁看到我的错在哪里?
答案 0 :(得分:0)
我想你想开始扫描活动并将结果带回MainActivity?如果我是对的。你犯了两个错误。 1.检查是否已初始化视图扫描按钮和箭头。 2.从您正在发送的扫描活动" scanContent" - > i.putExtra(" scanContent",内容); 在MainActivity中,您正在检查" message"中是否有任何数据。将返回false的键。删除此行 - >如果(extras.containsKey("消息&#34));并且您的代码将正常工作
(顺便说一句,而不是startActivity(扫描)你应该使用startActivityForResult(scan)并处理MainActivity的onActivityResult()中的结果)