我没有想法。我打电话给
for(ImageView imageView :((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList)
{
imageView.setClickable(true);
}
但是findFragmentByTag
返回null。调试器显示:
screenshot
当我更进一步时,程序会跳过所有的if并跳转返回null。
这是fragmentmanager
生成的日志输出04-21 19:12:27.492 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Commit: BackStackEntry{41050da0}
04-21 19:12:27.492 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: mName=null mIndex=-1 mCommitted=false
04-21 19:12:27.492 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Operations:
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Op #0: ADD MapFragment{410504e0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Op #1: ADD ParkFragment{410505e8 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Op #2: HIDE ParkFragment{410505e8 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Run: BackStackEntry{41050da0}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: add: MapFragment{410504e0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Allocated fragment index MapFragment{410504e0 #0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.499 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: add: ParkFragment{410505e8 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.507 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Allocated fragment index ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.507 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: hide: ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.507 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto CREATED: MapFragment{410504e0 #0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.507 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto CREATED: ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.507 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto ACTIVITY_CREATED: MapFragment{410504e0 #0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.554 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto ACTIVITY_CREATED: ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.906 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto STARTED: MapFragment{410504e0 #0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.914 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto STARTED: ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.914 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Commit: BackStackEntry{40feb250}
04-21 19:12:27.914 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: mName=null mIndex=-1 mCommitted=false
04-21 19:12:27.914 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Operations:
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Op #0: ADD BluetoothDialogFragment{410503d8 btDialog}
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto RESUMED: MapFragment{410504e0 #0 id=0x7f0c0071 MAPFRAGMENT}
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto RESUMED: ParkFragment{410505e8 #1 id=0x7f0c0071 PARKFRAGMENT}
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Run: BackStackEntry{40feb250}
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: add: BluetoothDialogFragment{410503d8 btDialog}
04-21 19:12:27.921 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Allocated fragment index BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:12:27.929 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto CREATED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:12:27.929 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto ACTIVITY_CREATED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:12:27.968 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto STARTED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:12:28.101 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: moveto RESUMED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.406 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Commit: BackStackEntry{420bc108}
04-21 19:13:30.406 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: mName=null mIndex=-1 mCommitted=false
04-21 19:13:30.406 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Operations:
04-21 19:13:30.421 26499-26499/com.example.paulforster.nxtapp D/FragmentManager: Op #0: REMOVE BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.929 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Run: BackStackEntry{420bc108}
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: remove: BluetoothDialogFragment{410503d8 #2 btDialog} nesting=0
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: movefrom RESUMED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: movefrom STARTED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: movefrom STOPPED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: movefrom ACTIVITY_CREATED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.937 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: movefrom CREATED: BluetoothDialogFragment{410503d8 #2 btDialog}
04-21 19:13:30.945 26499-26499/com.example.paulforster.nxtapp V/FragmentManager: Freeing fragment index BluetoothDialogFragment{410503d8 #2 btDialog}
这是整个MainActivity.java
:
package com.example.paulforster.nxtapp;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
import de.amr.plt.rcParkingRobot.AndroidHmiPLT;
import parkingRobot.INxtHmi;
//TODO einzeichnen von Fremnden Gegenständen in die Karte
//TODO warum kann ich da nicht parken?
//TODO ParkIcons neben die Linie
public class MainActivity extends AppCompatActivity {
static final int REQUEST_ENABLE_BT = 154;
static AndroidHmiPLT hmiModule = null;
static ArrayAdapter<String> BTArrayAdapter;
static BluetoothDialogFragment btDialog = null;
static Timer refreshTimer;
static TimerTask refreshTimerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
//Kein Bluetooth --> keine App
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth ist auf ihrem Gerät nicht verfügbar! :(",
Toast.LENGTH_LONG).show();
finish();
return;
}
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
for(int i=0; i<3; i++) {
bottomNavigationView.getMenu().getItem(i).setChecked(false);
}
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.mode_pause:
hmiModule.setMode(INxtHmi.Mode.PAUSE);
break;
case R.id.mode_scout:
hmiModule.setMode(INxtHmi.Mode.SCOUT);
break;
case R.id.mode_park:
hmiModule.setMode(INxtHmi.Mode.PARK_NOW);
Toast toast = Toast.makeText(getApplicationContext(),
"Nächste Parklücke wird angefahren", Toast.LENGTH_LONG);
toast.show();
break;
}
return false;
}
});
if (savedInstanceState == null) {
btDialog = new BluetoothDialogFragment();
MapFragment mapFragment = new MapFragment();
ParkFragment parkFragment = new ParkFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content, mapFragment, "MAPFRAGMENT");
ft.add(R.id.content, parkFragment, "PARKFRAGMENT");
ft.hide(parkFragment);
ft.commit();
} else {
MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT");
ParkFragment parkFragment = (ParkFragment) getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT");
}
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
}
@Override
protected void onStart() {
super.onStart();
Log.e("AConPause", "onStart");
reScheduleTimer(100);
}
@Override
protected void onPause() {
super.onStop();
Log.e("AConPause", "refreshTimer.cancel() happens");
refreshTimer.cancel();
refreshTimer = null;
}
@Override
protected void onResume() {
super.onResume();
Log.e("AConPause", "onResume");
//TODO wenn man gerade Bluetooth eingeschaltet hat, findet er die Liste noch nicht...
if (!btDialog.isAdded()) if(hmiModule == null) btDialog.show(getSupportFragmentManager(), "btDialog");
else if (!hmiModule.isConnected()) btDialog.show(getSupportFragmentManager(), "btDialog");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_ENABLE_BT){
if(resultCode != RESULT_OK){
finish();
}
}
}
public void reScheduleTimer(int duration) {
refreshTimer = new Timer();
refreshTimerTask = new MyTimerTask();
refreshTimer.schedule(refreshTimerTask, 0, duration);
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
refreshMenu();
}
});
}
}
public void refreshMenu(){
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (hmiModule != null)
if(hmiModule.isConnected()) {
Log.e("Status", "is verbunden");
if(hmiModule.getCurrentStatus() != null) {
Log.e("Status", "is da");
switch (hmiModule.getCurrentStatus()) {
case SCOUT:
bottomNavigationView.getMenu().getItem(0).setChecked(false);
bottomNavigationView.getMenu().getItem(1).setChecked(true);
bottomNavigationView.getMenu().getItem(2).setChecked(false);
bottomNavigationView.getMenu().getItem(0).setEnabled(true);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(true);
for(ImageView imageView : ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList){
imageView.setClickable(true);
}
fragmentTransaction.hide((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
if ((((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler != null){
String data = "MSG DATA";
Message msg = (((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler.obtainMessage(0, data);
msg.sendToTarget();
}
break;
case PARK_NOW:
bottomNavigationView.getMenu().getItem(0).setChecked(false);
bottomNavigationView.getMenu().getItem(1).setChecked(false);
bottomNavigationView.getMenu().getItem(2).setChecked(true);
bottomNavigationView.getMenu().getItem(0).setEnabled(true);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(true);
for(ImageView imageView : ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList){
imageView.setClickable(true);
}
if ((((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler != null){
String data = "MSG DATA";
Message msg = (((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler.obtainMessage(0, data);
msg.sendToTarget();
}
fragmentTransaction.show((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
/**
if(getSupportFragmentManager().getBackStackEntryCount()==0){
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ParkFragment parkFragment = new ParkFragment();
ft.add(R.id.content, parkFragment);
ft.addToBackStack(null);
ft.commit();
}*/
break;
case PARK_THIS:
bottomNavigationView.getMenu().getItem(0).setChecked(false);
bottomNavigationView.getMenu().getItem(1).setChecked(false);
bottomNavigationView.getMenu().getItem(2).setChecked(true);
bottomNavigationView.getMenu().getItem(0).setEnabled(true);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(true);
for(ImageView imageView : ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList){
imageView.setClickable(true);
}
fragmentTransaction.show((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
/**
if(getSupportFragmentManager().getBackStackEntryCount()==0){
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ParkFragment parkFragment = new ParkFragment();
ft.add(R.id.content, parkFragment);
ft.addToBackStack(null);
ft.commit();
}
*/
break;
case PARKED:
bottomNavigationView.getMenu().getItem(0).setChecked(true);
bottomNavigationView.getMenu().getItem(1).setChecked(false);
bottomNavigationView.getMenu().getItem(2).setChecked(false);
bottomNavigationView.getMenu().getItem(0).setEnabled(false);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(false);
for(ImageView imageView : ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList){
imageView.setClickable(false);
}
fragmentTransaction.hide((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
if ((((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler != null){
String data = "MSG DATA";
Message msg = (((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler.obtainMessage(0, data);
msg.sendToTarget();
}
break;
case INACTIVE:
bottomNavigationView.getMenu().getItem(0).setChecked(true);
bottomNavigationView.getMenu().getItem(1).setChecked(false);
bottomNavigationView.getMenu().getItem(2).setChecked(false);
bottomNavigationView.getMenu().getItem(0).setEnabled(true);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(true);
//TODO die Arraylist mit den ParkIcons muss hier immernoch existieren
for(ImageView imageView : ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList){
imageView.setClickable(true);
}
fragmentTransaction.hide((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
if ((((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler != null){
String data = "MSG DATA";
Message msg = (((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler.obtainMessage(0, data);
msg.sendToTarget();
}
break;
}
}
} else {
for(int i = 0; i<3; i++){
bottomNavigationView.getMenu().getItem(i).setChecked(false);
bottomNavigationView.getMenu().getItem(i).setEnabled(false);
getSupportFragmentManager().popBackStack();
}
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (hmiModule != null && hmiModule.connected) {
terminateBluetoothConnection();
}
}
/**
* Terminate the bluetooth connection to NXT
*/
private void terminateBluetoothConnection(){
Toast.makeText(this, "Bluetooth connection was terminated!", Toast.LENGTH_LONG).show();
hmiModule.setMode(INxtHmi.Mode.PAUSE);
hmiModule.setMode(INxtHmi.Mode.DISCONNECT);
hmiModule.disconnect();
while(hmiModule.isConnected()){
//wait until disconnected
}
hmiModule = null;
}
}
有人暗示我?谢谢!
修改 错误不再发生。 我的目标是使设备旋转成为可能。我做了一些撤消/更改,可以在commit中找到。
也许有人知道会发生什么。
答案 0 :(得分:0)
在提交片段事务后添加此行(onCreate中的ft.commit())。
getSupportFragmentManager().executePendingTransactions();
调用commit()不会立即执行事务。相反,一旦线程能够这样做,它就会将其安排在活动的UI线程(&#34; main&#34;线程)上运行。但是,如果需要,您可以从UI线程调用executePendingTransactions()以立即执行commit()提交的事务。除非事务是其他线程中作业的依赖项,否则通常不需要这样做。
有关executePendingTransactions的详细信息,请参阅https://developer.android.com/guide/components/fragments.html#Transactions。
答案 1 :(得分:0)
试试这个,
public void refreshMenu(){
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
List<ImageView> imageViewList = ((MapFragment)getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT")).imageviewArrayList;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (hmiModule != null)
if(hmiModule.isConnected()) {
Log.e("Status", "is verbunden");
if(hmiModule.getCurrentStatus() != null) {
Log.e("Status", "is da");
switch (hmiModule.getCurrentStatus()) {
case SCOUT:
bottomNavigationView.getMenu().getItem(0).setChecked(false);
bottomNavigationView.getMenu().getItem(1).setChecked(true);
bottomNavigationView.getMenu().getItem(2).setChecked(false);
bottomNavigationView.getMenu().getItem(0).setEnabled(true);
bottomNavigationView.getMenu().getItem(1).setEnabled(true);
bottomNavigationView.getMenu().getItem(2).setEnabled(true);
for(ImageView imageView :imageViewList){
imageView.setClickable(true);
}
fragmentTransaction.hide((ParkFragment)getSupportFragmentManager().findFragmentByTag("PARKFRAGMENT"));
fragmentTransaction.commit();
if ((((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler != null){
String data = "MSG DATA";
Message msg = (((MapFragment) getSupportFragmentManager().findFragmentByTag("MAPFRAGMENT"))).mHandler.obtainMessage(0, data);
msg.sendToTarget();
}
break;
我怀疑你的代码中的问题是你的beginTransaction与FragmentManager之间,你正在查询它的FindFragmentByTag,因此我将它移动到beginTransaction的顶部。在所有使用的地方进行类似的更改,并让我知道您的反馈