Android:Fragment却看不到ActionBar

时间:2015-02-12 21:06:31

标签: android android-fragments view android-actionbar fragment

长时间听众,第一次海报。我是Android的新手,但我一直在努力通过Big Ranch Guide示例来努力提高我的理解力。

我正在尝试在以下课程中显示操作栏。

public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField; 
    private Button mDateButton;
    private Button mTimeButton; 
    private CheckBox mSolvedCheckedBox;
    public static final String EXTRA_CRIME_ID =    "com.example.criminalIntent";
   private static final String DIALOG_DATE = "date";
   private static final String DIALOG_TIME = "time";
   private static final int REQUEST_DATE = 0;
   private static final int REQUEST_TIME = 1;

@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
    setHasOptionsMenu(true);
}

public void updateDate() {
    SimpleDateFormat datFormat = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat timFormat = new SimpleDateFormat("hh:mm:ss zzz");
    mDateButton.setText(datFormat.format(mCrime.getDate()));
    mTimeButton.setText(timFormat.format(mCrime.getDate()));

}

@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_crime, parent, false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        if (NavUtils.getParentActivityName(getActivity()) != null) {

            if (getActivity().getActionBar() == null){
                Log.d("CrimeFragment", "Sh*t!!");
            } else

            getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
            //((ActionBarActivity)getActivity()).getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    mTitleField = (EditText)v.findViewById(R.id.crime_title);
    mTitleField.setText(mCrime.getTitle());
    mTitleField.addTextChangedListener(new TextWatcher() {

        public void onTextChanged (CharSequence c, int start, int before, int count){
            mCrime.setTitle(c.toString());
        }

        public void beforeTextChanged (CharSequence c, int start, int before, int count){
            //leave blank
        }

        public void afterTextChanged(Editable c) {
            //leave blank
        }
    });


    mDateButton = (Button)v.findViewById(R.id.crime_date); 
    mDateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate());
            dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
            dialog.show(fm, DIALOG_DATE);

        }
    });

    mTimeButton = (Button)v.findViewById(R.id.crime_time); 
    mTimeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            TimePickerFragment dialog = TimePickerFragment.newInstance(mCrime.getDate());
            dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME);
            dialog.show(fm, DIALOG_TIME);

        }
    });

    mSolvedCheckedBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckedBox.setChecked(mCrime.getSolved());
    mSolvedCheckedBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
            mCrime.setSolved(isChecked);
        }
    });
    updateDate();
    return v;
}

//This adds a crime unique identifier to the fragment for retrieval by CrimeActivity
public static CrimeFragment newInstance(UUID crimeId) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment(); 
    fragment.setArguments(args);

    return fragment;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        //to be implemented next

        if (NavUtils.getParentActivityName(getActivity())!= null){
            NavUtils.navigateUpFromSameTask(getActivity());
        }   
            return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    final SimpleDateFormat df = new SimpleDateFormat(("MM/DD/yyyy"));
    if (resultCode != Activity.RESULT_OK){
        return;
    } 
    if (requestCode == REQUEST_DATE) {
        Date date = (Date)data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
        mCrime.setDate(date);
        updateDate();
    }
    if (requestCode == REQUEST_TIME) {
        Date date = (Date)data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
        mCrime.setDate(date);
        updateDate();
    }
}

}

StackOverflow在为他的问题提供大量解决方案方面提供了令人难以置信的帮助。我尝试了以下内容。

  1. 通过ActionBarActivity进行扩展:使用onCreate创建Havoc。
  2. 更新清单文件以包含带有操作栏的主题;做了@ style / Theme.AppCompat.Light.DarkActionBar
  3. 尝试将ActionBar转换为(ActionBarActivity):没有工作
  4. 尝试使用getSupportActionBar():没有工作
  5. 尝试项目名称=" android:windowActionBar"在styles.xml中为true;我试过这个。
  6. 从代码中我可以看到,我也在API11上定位了视图功能。任何帮助将不胜感激。

    感谢您的评论,它是从CrimeListActivity中的菜单调用,如下所示。

    public class CrimeListFragment extends ListFragment {
    private ArrayList<Crime> mCrimes;
    private static final String TAG = "CrimeListFragment";
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
                //getActivity().getActionBar().show();
        getActivity().setTitle(R.string.crimes_title); 
        mCrimes = CrimeLab.get(getActivity()).getCrimes();
    
    
        CrimeAdapter adapter = new CrimeAdapter(mCrimes);
        setListAdapter(adapter);
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        Crime c = (Crime)(getListAdapter()).getItem(position);
        Log.d(TAG, c.getTitle() + " was clicked");
    
        //Start CriminalPagerActivity
        Intent i = new Intent(getActivity(), CrimePagerActivity.class);
        i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
        startActivity(i); 
    }
    
    private class CrimeAdapter extends ArrayAdapter<Crime> {
    
        public CrimeAdapter(ArrayList<Crime> crimes){
            super(getActivity(), 0, crimes);
        }
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // if no view, inflate one. 
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);
            }
    
            //Configure the view for this Crime
            Crime c = mCrimes.get(position); 
    
            TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
            titleTextView.setText(c.getTitle());
            TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);
            dateTextView.setText(c.getDate().toString());
            CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
            solvedCheckBox.setChecked(c.getSolved());
    
            return convertView;
            }
        }
    
    @Override
    public void onResume() {
        super.onResume();
        ((CrimeAdapter)getListAdapter()).notifyDataSetChanged(); 
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_list, menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    
        switch(item.getItemId()) {
        case R.id.menu_item_new_crime:
            Crime crime = new Crime(); 
            CrimeLab.get(getActivity()).addCrime(crime);
            Intent i = new Intent(getActivity(), CrimePagerActivity.class);
            i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
            startActivityForResult(i, 0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    
        }
    }
    
    }
    

0 个答案:

没有答案