android - 关于我的片段和活动的奇怪行为
问题描述
我有一个读取 NFC 标签的应用程序(当单击 MainActivity 中的主按钮时),然后以模式显示结果。它工作得很好,但知道,我想做的是创建一个片段来使用 ViewPager(在我未来的片段上有很好的过渡)。
但现在,起初,我无法按下我的主按钮。然后,当我扫描标签时,它会将我置于其他视图(或者我不知道是什么,我不完全理解),并以模式显示结果。此时,我在我的应用程序的右侧,具有相同的视图。
在此之后,我可以点击我的主按钮并扫描标签,但是,我在错误的片段或类似的东西上,我只能去左边
这是我的代码项目:https ://github.com/elgrusko/nfc_stackoverflow
或者我的主要代码:
package com.example.poissonpane;
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 2;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
public static int display, result, detect = 0;
private NfcAdapter nfcAdapter;
private ImageButton nfcButton;
private PendingIntent pendingIntent;
public static IntentIntegrator integrator;
public ScreenSlidePageFragment scan_fragment;
public static String link = null;
public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.d("TOTO", Integer.toString(position));
switch (position) {
case 0:
return new ScreenSlidePageFragment();
case 1:
return new SecondFragment();
default:
return new ScreenSlidePageFragment();
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
public void onResume() {
super.onResume();
Log.d("TOTO", "onResume() main activity");
Intent intent = getIntent(); // Recuperer l'intent actuel
NdefMessage[] msgs;
// If a NFC tag has been detected once app was closed
if (detect == 1 && (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))) { // si l'application a ete lancee via une detection NFC du device (ACTION_NDEF_DISCOVERED, voir AndroidManifest)
Parcelable[] rawMessages =
intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
if (rawMessages != null) { // if NDEF in scan
display = 1;
resolveIntent(intent); // parser NFC scan
} else {
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = NdefMessageParser.dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
detect = 0;
scan_fragment.getResult(msgs);
}
}
NfcAdapter nfcAdapter2 = this.nfcAdapter;
if (nfcAdapter2 != null) {
if (!nfcAdapter2.isEnabled()) {
showWirelessSettings();
}
this.nfcAdapter.enableForegroundDispatch(this, this.pendingIntent, (IntentFilter[]) null, (String[][]) null);
}
}
private void showWirelessSettings() { // ask user to enable NFC on his devices
Toast.makeText(this, "Veuillez activer le service NFC", Toast.LENGTH_SHORT).show();
startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
}
public void onPause() {
Log.d("TOTO", "onPause() MainActivity");
detect = 0;
super.onPause();
}
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
if (detect != 1) {
resolveIntent(intent);
}
}
private void resolveIntent(Intent intent) { // DETECT NFC & PARSING NDEF
NdefMessage[] msgs;
String action = intent.getAction();
Log.d("Infos", getIntent().toString());
Log.d("Infos", "display = " + display);
if (display != 1) {
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
nfcButton.startAnimation(shake);
Toast.makeText(this, "Veuillez lancer un scan", Toast.LENGTH_LONG).show();
return;
}
else if ("android.nfc.action.TAG_DISCOVERED".equals(action) || "android.nfc.action.TECH_DISCOVERED".equals(action) || "android.nfc.action.NDEF_DISCOVERED".equals(action)) {
result = 1;
Parcelable[] rawMsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = NdefMessageParser.dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
}
if (display == 1) {
scan_fragment.getResult(msgs);
}
}
}
public void ScanResultModal(String link, StringBuilder tmp_builder) { // we don't care for the moment
Log.d("Toto", "ScanResultModal");
/* Intent i = new Intent(MainActivity.this, RightActivity.class);
link = tmp_builder.toString();
i.putExtra("url", link);
startActivity(i);*/
}
}
package com.example.poissonpane;
import java.util.List;
import static com.example.poissonpane.MainActivity.link;
public class ScreenSlidePageFragment extends Fragment {
public MainActivity mainActivity;
private ImageButton nfcButton;
ObjectAnimator anim;
ObjectAnimator anim2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
mainActivity = (MainActivity) getActivity();
Log.d("TOTO", "onCreateView() Fragment");
MainActivity.display = 1;
anim = ObjectAnimator.ofFloat(nfcButton, "scaleX", new float[]{0.9f});
anim2 = ObjectAnimator.ofFloat(nfcButton, "scaleY", new float[]{0.9f});
nfcButton = (ImageButton) rootView.findViewById(R.id.nfc_button);
nfcButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.display = 1;
manageBlinkEffect();
}
});
return rootView;
}
public void manageBlinkEffect() { // Just to make grow and shrink the Button while scanning // ANIMATION DU LOGO SENZU SUITE A UN CLIC
anim.setDuration(1000);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(9);
anim.start();
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationCancel(animation);
if (MainActivity.result != 1){
Animation shake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);
Toast.makeText(getContext(), "Aucun badge n'a été détécté !", Toast.LENGTH_SHORT).show();
nfcButton.startAnimation(shake);
}
}
});
anim2.setDuration(1000);
anim2.setRepeatMode(ValueAnimator.REVERSE);
anim2.setRepeatCount(9);
anim2.start();
}
public void getResult(NdefMessage[] msgs) { // MODAL RESULT SCAN NFC
if (msgs != null && msgs.length != 0) {
final StringBuilder tmp_builder = new StringBuilder();
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
int size = records.size();
for (int i = 0; i < size; i++) {
tmp_builder.append(records.get(i).str());
tmp_builder.append("\n");
}
System.out.println(tmp_builder.toString());
this.anim.cancel();
this.anim2.cancel();
MainActivity.display = 0;
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Resultat du scan :");
builder.setNeutralButton("Visiter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((MainActivity)getActivity()).ScanResultModal(link, tmp_builder);
}
});
builder.setNegativeButton("Copier", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Texte copié", link);
clipboard.setPrimaryClip(clip);
}
});
builder.setPositiveButton("Fermer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
});
builder.setMessage(tmp_builder.toString());
builder.create().show();
}
}
}
您在我的片段中看到什么问题或其他什么吗?
对不起,解释。真的很难解释
谢谢 !
解决方案
推荐阅读
- jenkins - 如何修复这个 Jenkins 流水线?
- linux - Yocto 抱怨两个食谱安装了相同的头文件
- ruby-on-rails - 如何复制/复制有很多关联和更新少数列
- python - 如何遍历字符串列表并识别整数和浮点数,然后将它们添加到列表中?
- reactjs - Typeerror myFunction.map 不是函数
- python - python 正则表达式重复模式
- reactjs - Gatsby 博客最后一页的分页禁用按钮
- android - 运行 main.dart I/flutter (15748) 后无法获得输出:天文台正在监听 ****************************** ******
- ios - SwiftUI ScrollView 根据下拉菜单的变化以编程方式滚动到顶部
- testing - 如何为 Rust 二进制包编写回归测试?