我正在尝试创建一个应用程序,显示我在谷歌地图中的位置。然后我有一些搜索栏改变了什么日子,在那段时间我想表明我曾经在哪里。我有一个代码工作,当我有googlemaps片段和搜索栏(使用晶体搜索栏)同样的活动共享屏幕上的空间。但是现在我想增加googlemap的大小,而是在点击一个名为settings的按钮时,通过对话框显示其他布局中的搜索栏。我不能让它工作,因为它抱怨视图,我不想删除视图,因为那时我的所有参数和东西都会变回sarting值并停止工作。下面我展示了我的类代码和错误消息。
这里主要活动中的搜索栏声明: @覆盖 protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);
//Initialize
InitializeUI();
//Makes buttons work
onClickListener();
//UpdateGuiStart
updateStartGUI();
//Seekbars
SeekBarCheckBoxModeController();
}
这是我开始使用所有搜索栏的地方:
public void InitializeUI() {
GPSButton = (Button) findViewById(R.id.GPSButton);
mapsButton = (Button) findViewById(R.id.mapsButton);
TextSpeed = (TextView) findViewById(R.id.TextSpeed);
check_view = (TextView) findViewById(R.id.textView_check);
deleteData = (Button) findViewById(R.id.button_delete);
settings = (Button) findViewById(R.id.button_settings);
TextSpeed.setText("Speed : " + speed);
mModeSpinner = (Spinner) layout_settings.findViewById(R.id.spinner);
mSeekBarTime = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTime);
mSeekBarDate = (CrystalRangeSeekbar) layout_settings.findViewById(R.id.rangeSeekbarDate);
mSeekBarTimeLength = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTimeLength);
mSeekBarTransparency = (CrystalSeekbar) layout_settings.findViewById(R.id.rangeSeekbarTransparency);
tvMinTime = (TextView) layout_settings.findViewById(R.id.textViewMinTime);
tvMaxTime = (TextView) layout_settings.findViewById(R.id.textViewMaxTime);
tvMinDate = (TextView) layout_settings.findViewById(R.id.textViewMinDate);
tvMaxDate = (TextView) layout_settings.findViewById(R.id.textViewMaxDate);
tvMaxDates = (TextView) layout_settings.findViewById(R.id.textViewMaxDates);
tvTransparency = (TextView) layout_settings.findViewById(R.id.textViewTransparency);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Initialize database helper
mDbHelper = new LocationDbHelper(this);
}
这是我的alertdialog,用于显示我想要在地图上进行更改的搜索栏,当我再次点击那里时,我希望我的搜索栏保持与上次更改时保留的相同方式。 ..
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
minday = current_minday;
maxday = current_maxday;
mintime = current_time;
maxtime = maxTime;
trans = current_trans;
mode = mModeSpinner.getSelectedItemPosition();
final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
settings_alert.setView(layout_settings);
settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
show();
Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
dialogInterface.dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSeekBarDate.setMaxStartValue(maxday);
mSeekBarDate.setMinStartValue(minday);
mSeekBarTime.setMinStartValue(mintime);
mSeekBarTimeLength.setMinStartValue(maxtime);
mModeSpinner.setSelection(mode);
dialogInterface.dismiss();
}
}).show();
}
});
错误讯息:
FATAL EXCEPTION: main
Process: com.example.smartcities.smartcitiesproject, PID: 30988
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4654)
at android.view.ViewGroup.addView(ViewGroup.java:4490)
at android.view.ViewGroup.addView(ViewGroup.java:4462)
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:613)
at android.support.v7.app.AlertController.setupView(AlertController.java:452)
at android.support.v7.app.AlertController.installContent(AlertController.java:215)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
at android.app.Dialog.show(Dialog.java:314)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
at com.example.smartcities.smartcitiesprojects.MainActivity$4.onClick(MainActivity.java:313)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);
以上代码必须是onClick事件的本地代码。
应该是这样的:
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout_settings = (LinearLayout)inflater.inflate(R.layout.dialog_settings, null);
minday = current_minday;
maxday = current_maxday;
mintime = current_time;
maxtime = maxTime;
trans = current_trans;
mode = mModeSpinner.getSelectedItemPosition();
final AlertDialog.Builder settings_alert = new AlertDialog.Builder(MainActivity.this);
settings_alert.setView(layout_settings);
settings_alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
show();
Toast.makeText(MainActivity.this, "Settings changed", Toast.LENGTH_LONG).show();
dialogInterface.dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mSeekBarDate.setMaxStartValue(maxday);
mSeekBarDate.setMinStartValue(minday);
mSeekBarTime.setMinStartValue(mintime);
mSeekBarTimeLength.setMinStartValue(maxtime);
mModeSpinner.setSelection(mode);
dialogInterface.dismiss();
}
}).show();
}
});