[解决] 应用程序工作正常而不会崩溃,但应该将结果视图文本从“Hasil扫描”更新为扫描结果,但它不会。问题是textview(resultView)在扫描后没有更新。我正在使用DM77 Zxing条码扫描器。 这是我到目前为止所做的代码。
MainActivity:
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import sc.smklabor.rpl.labsscanner.sc.smklabor.rpl.labsscanner.fragments.ScanFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.scannav:
selectedFragment = ScanFragment.instance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, ScanFragment.instance());
transaction.commit();
}
}
这里是扫描片段,我只做了一个片段,其他片段就是和网站有关 ScanFragment:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.TextView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import sc.smklabor.rpl.labsscanner.R;
/**
* Created by Lenovo on 21/03/2018.
*/
public class ScanFragment extends Fragment implements View.OnClickListener {
private IntentIntegrator scanner;
private Button startScan,btnGoUrl;
private TextView resultView;
private String scanResult;
public static ScanFragment instance() {
ScanFragment f = new ScanFragment();
return f;
}
public void onCreate(Bundle b) {
super.onCreate(b);
scanner = new IntentIntegrator(this.getActivity()).forSupportFragment(this);
}
public void onActivityResult(int rc,int res, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(rc, res, data);
if(result!=null) {
scanResult = result.getContents().toString();
if (scanResult.contains("--L--:")) {
String postId = scanResult.split("://")[1];
btnGoUrl.setVisibility(View.INVISIBLE);
resultView.setText("Post ID: " + postId);
} else if (URLUtil.isHttpUrl(scanResult) || isValidURL(scanResult)) {
btnGoUrl.setVisibility(View.VISIBLE);
resultView.setText(result.getContents().toString());
} else {
btnGoUrl.setVisibility(View.INVISIBLE);
resultView.setText(result.getContents().toString());
}
}
}
public View onCreateView(LayoutInflater inf, ViewGroup con, Bundle b) {
View view = inf.inflate(R.layout.scanlayout, con, false);
startScan = (Button)view.findViewById(R.id.button);
btnGoUrl = (Button)view.findViewById(R.id.btnGoUrl);
scanner = new IntentIntegrator(getActivity());
startScan.setOnClickListener(this);
resultView = (TextView)view.findViewById(R.id.textView);
btnGoUrl.setOnClickListener(new Button.OnClickListener() {
public void onClick(View e) {
Uri URL = Uri.parse(scanResult);
Intent i = new Intent(Intent.ACTION_VIEW,URL);
startActivity(i);
}
});
return view;
}
public boolean isValidURL(String s) {
return (s.contains("http://") || s.contains("https://")) && (s.contains(".com") || s.contains(".org") || s.contains(".net") || s.contains(".id") || s.contains(".sch.id") || s.contains(".me"));
}
public void onClick(View e) {
scanner.setPrompt("Arahkan kamera kepada barcode tepat pada kotak.");
scanner.setOrientationLocked(false);
scanner.initiateScan();
}
}
答案 0 :(得分:1)
我认为问题是onActivityResult
ScanFragment
首先你应该覆盖onActivityResult(int rc,int res, Intent data)
在MainActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
此代码可帮助您致电onActivityResult
上的ScanFragment
。