0

I have ceated a tabbed application using Tab host. There are 5 tabs and the first tab contains one fragment. In that fragment I am trying to show date/time picker upon clicking a particular view. But the app crashes with following error.

I am using CiTux datetime picker.

I have tried a demo app without using tab host and it works great. But it doesn't work while using tabs in my app. Please help with any solutions..

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(Child activity)

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();
        }
    }
}


}
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
Satyasarathi
  • 93
  • 2
  • 10

1 Answers1

0

Finally I got the answer. That is the getFragmentManager(), which we are passing in show() method of dialog. We need to create the getFragmentManager() from the TabHost Activity as the dialog try to get the context from it. So it doesn't found the right context object.

DatePickerDialog.newInstance(
                MainActivity.tabContext,
                dateNow.get(Calendar.YEAR),
                dateNow.get(Calendar.MONTH),
                dateNow.get(Calendar.DAY_OF_MONTH)
        );

        dpd.show(MainActivity.tabContext.getFragmentManager(), "Datepickerdialog");

Declare an static instance of your TabHost activity and get the job done. Happy coding.

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();

}
Satyasarathi
  • 93
  • 2
  • 10