我已使用Tab主机停止了选项卡式应用程序。有5个选项卡,第一个选项卡包含一个片段。在该片段中,我试图在单击特定视图时显示日期/时间选择器。但是应用程序因以下错误而崩溃。
我正在使用CiTux datetime picker。
我尝试过不使用标签主机的演示应用,效果很好。但是在我的应用中使用标签时它不起作用。请帮助解决任何问题..
FATAL EXCEPTION: main
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: Process: com.sample, PID: 5143
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@fb56afd is not valid; is your activity running?
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.Dialog.show(Dialog.java:298)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.DialogFragment.onStart(DialogFragment.java:490)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.Fragment.performStart(Fragment.java:2077)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:834)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-24 16:50:43.195 5143-5143/com.sample E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
EntryTabGroupActivity.java
package com.sample.view.tab;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.android.datetimepicker.date.DatePickerDialog;
import com.sample.R;
import com.sample.view.entry.NewEntryActivity;
import com.sample.view.splash.AJApplication;
public class EntryTabActivity extends TabgroupActivity{
private static final String TAG=EntryTabActivity.class.getSimpleName();
AJApplication application;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
application=AJApplication.getInstance();
Intent intent=new Intent(application.getApplicationContext(), NewEntryActivity.class);
startChildActivity(TAG,intent);
}
}
NewEntryActivity.java(子活动)
package com.sample.view.entry;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.android.datetimepicker.date.DatePickerDialog;
import com.android.datetimepicker.time.RadialPickerLayout;
import com.android.datetimepicker.time.TimePickerDialog;
import com.sample.R;
import com.sample.entry.NewEntryPresenter;
import com.sample.entry.NewEntryPresenterImpl;
import com.sample.util.SegmentedGroup;
import com.sample.view.splash.AJApplication;
import java.util.Calendar;
public class NewEntryActivity extends Activity implements
NewEntryView,RadioGroup.OnCheckedChangeListener,OnFragmentInteractionListener,DatePickerDialog.OnDateSetListener,TimePickerDialog.OnTimeSetListener{
private static final String TAG=NewEntryActivity.class.getSimpleName();
AJApplication application=AJApplication.getInstance();
TextView titleTxt;
NewEntryPresenter presenter;
SegmentedGroup foodSympSegment,sympLevelSegment;
RadioButton foodSegmentBtn,sympSegmentBtn,mildSegmentBtn,mediumSegmentBtn,severeSegmentBtn;
private Calendar calendar;
public static DatePickerDialog datePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
setContentView(R.layout.activity_new_entry);
application=AJApplication.getInstance();
titleTxt= (TextView) findViewById(R.id.HEADER_TITLE);
foodSympSegment= (SegmentedGroup) findViewById(R.id.foodSympSegment);
foodSegmentBtn= (RadioButton) findViewById(R.id.buttonFood);
sympSegmentBtn= (RadioButton) findViewById(R.id.buttonSymptom);
foodSympSegment.setOnCheckedChangeListener(this);
presenter=new NewEntryPresenterImpl(this);
presenter.init();
}
@Override
public void setupUI() {
Log.i(TAG, "setupUI()");
application.setHeader(this, false, false, false, true, false, false, false, false, false);
titleTxt.setText(getString(R.string.new_entry));
foodSegmentBtn.setChecked(true);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.i(TAG, "onCheckedChanged()");
if(checkedId==foodSegmentBtn.getId()){
presenter.loadFoodSegment();
}else if (checkedId==sympSegmentBtn.getId()){
presenter.loadSymptomSegment();
}
}
@Override
public void displayFoodUI() {
Log.i(TAG, "displayFoodUI()");
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
/*
* Create Food fragment
*/
FoodFragment fragment=new FoodFragment();
transaction.replace(R.id.fragContainer, fragment);
transaction.addToBackStack("FoodFragment").commit();
}
@Override
public void displaySymptomUI() {
Log.i(TAG, "displaySymptomUI()");
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
/*
* Create Food fragment
*/
SymptomFragment fragment=new SymptomFragment();
transaction.replace(R.id.fragContainer, fragment);
transaction.addToBackStack("SymptomFragment").commit();
}
@Override
public void onFragmentInteraction(Uri uri) {
Log.i(TAG, "onFragmentInteraction()");
}
@Override
public void onDateSet(DatePickerDialog datePickerDialog, int year, int monthOfYear , int dayOfMonth) {
Log.i(TAG, "onDateSet()");
}
@Override
public void onTimeSet(RadialPickerLayout radialPickerLayout, int hourOfDay, int minute) {
Log.i(TAG,"onTimeSet()");
}
}
FoodFragment.java
package com.sample.view.entry;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.android.datetimepicker.date.DatePickerDialog;
import com.android.datetimepicker.time.RadialPickerLayout;
import com.android.datetimepicker.time.TimePickerDialog;
import com.sample.R;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class FoodFragment extends Fragment implements View.OnClickListener {
private static final String TAG=FoodFragment.class.getSimpleName();
public OnFragmentInteractionListener mListener;
private static final String TIME_PATTERN = "HH:mm";
private Calendar calendar;
private DateFormat dateFormat;
private SimpleDateFormat timeFormat;
TextView dateTimeTxtvw;
TextView foodEntryTxtvw;
EditText foodNotesTxtvw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"onCreate()");
calendar = Calendar.getInstance();
dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
timeFormat = new SimpleDateFormat(TIME_PATTERN, Locale.getDefault());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG,"onCreateView()");
View inflaterView=inflater.inflate(R.layout.fragment_food, container, false);
dateTimeTxtvw= (TextView) inflaterView.findViewById(R.id.foodDateTimeTxtvw);
foodEntryTxtvw= (TextView) inflaterView.findViewById(R.id.foodEntryTxtvw);
foodNotesTxtvw= (EditText) inflaterView.findViewById(R.id.foodNotesTxt);
updateTime();
dateTimeTxtvw.setOnClickListener(this);
return inflaterView ;
}
public void onButtonPressed(Uri uri) {
Log.i(TAG,"onButtonPressed()");
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
private void updateTime(){
Log.i(TAG,"updateTime()");
}
@Override
public void onAttach(Context context) {
Log.i(TAG, "onAttach()");
super.onAttach(context);
try {
mListener = (OnFragmentInteractionListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
Log.i(TAG, "onDetach()");
mListener = null;
}
@Override
public void onClick(View v) {
Log.i(TAG,"onClick()");
if(v.getId()==dateTimeTxtvw.getId()){
try {
DatePickerDialog.newInstance((DatePickerDialog.OnDateSetListener) getActivity(),
calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH))
.show(getFragmentManager(), "datepicker");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
最后我得到了答案。这就是getFragmentManager(),我们在对话框的show()方法中传递它。我们需要从TabHost Activity创建getFragmentManager(),因为对话框尝试从中获取上下文。所以它找不到合适的上下文对象。
DatePickerDialog.newInstance(
MainActivity.tabContext,
dateNow.get(Calendar.YEAR),
dateNow.get(Calendar.MONTH),
dateNow.get(Calendar.DAY_OF_MONTH)
);
dpd.show(MainActivity.tabContext.getFragmentManager(), "Datepickerdialog");
声明TabHost活动的静态实例并完成工作。快乐的编码。
public class MainActivity extends TabActivity implements TimePickerDialog.OnTimeSetListener,DatePickerDialog.OnDateSetListener{
private static final String TAG=MainActivity.class.getSimpleName();
AJApplication application;
TabHost tabHost;
public static MainActivity tabContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
setContentView(R.layout.activity_main);
application=AJApplication.getInstance();
tabContext=this;
tabHost=getTabHost();
}