我正在尝试将此日历应用添加到标签操作栏中。但是,当我将所有代码加入到一起尝试完成应用程序时,我会遇到很多错误。
引起:android.content.ActivityNotFoundException:无法找到显式活动类
它告诉我,我应该尝试将它添加到android清单中,但因为它们是片段肯定你不能将片段添加到android清单中,无论如何这里是我的代码......
package com.idkstudios.shiftcalculator;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import com.idkstudios.shiftcalculator.tab.TabsPagerAdapter;
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter adapter;
private ActionBar actionBar;
/**
* Tab titles
*/
private String[] tabTitles = {"Hours List", "Calendar", "Pay"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
adapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/**
* For adding tabs
*/
for(String tabs : tabTitles){
actionBar.addTab(actionBar.newTab().setText(tabs).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}
这是我的主要活动,这是三个片段,
package com.idkstudios.shiftcalculator.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import com.idkstudios.shiftcalculator.R;
import com.idkstudios.shiftcalculator.calendar.adapter.CalendarAdapter;
import com.idkstudios.shiftcalculator.calendar.util.CalendarCollection;
import java.util.GregorianCalendar;
/**
* Created by Toby on 02/09/2015.
*/
public class CalendarFragment extends Fragment {
/**
* The basic variables that you will need for the calendar
*/
public GregorianCalendar cal_month, cal_month_copy;
private CalendarAdapter cal_adapter;
private TextView tv_month;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* Sets up the basic layout file, we return this later
*/
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
cal_month = (GregorianCalendar) GregorianCalendar.getInstance();
cal_month_copy = (GregorianCalendar) cal_month.clone();
cal_adapter = new CalendarAdapter(getActivity(), cal_month, CalendarCollection.date_collection_arr);
tv_month = (TextView) getActivity().findViewById(R.id.tv_month);
tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
ImageButton previous = (ImageButton) getActivity().findViewById(R.id.ib_prev);
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPreviousMonth();
refreshCalendar();
}
});
ImageButton next = (ImageButton) getActivity().findViewById(R.id.Ib_next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setNextMonth();
refreshCalendar();
}
});
GridView gridview = (GridView) getActivity().findViewById(R.id.gv_calendar);
gridview.setAdapter(cal_adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
((CalendarAdapter) parent.getAdapter()).setSelected(v,position);
String selectedGridDate = CalendarAdapter.day_string
.get(position);
String[] separatedTime = selectedGridDate.split("-");
String gridvalueString = separatedTime[2].replaceFirst("^0*","");
int gridvalue = Integer.parseInt(gridvalueString);
if ((gridvalue > 10) && (position < 8)) {
setPreviousMonth();
refreshCalendar();
} else if ((gridvalue < 7) && (position > 28)) {
setNextMonth();
refreshCalendar();
}
((CalendarAdapter) parent.getAdapter()).setSelected(v,position);
((CalendarAdapter) parent.getAdapter()).getPositionList(selectedGridDate, getActivity());
}
});
return rootView;
}
protected void setNextMonth() {
if (cal_month.get(GregorianCalendar.MONTH) == cal_month
.getActualMaximum(GregorianCalendar.MONTH)) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) + 1),
cal_month.getActualMinimum(GregorianCalendar.MONTH), 1);
} else {
cal_month.set(GregorianCalendar.MONTH,
cal_month.get(GregorianCalendar.MONTH) + 1);
}
}
protected void setPreviousMonth() {
if (cal_month.get(GregorianCalendar.MONTH) == cal_month
.getActualMinimum(GregorianCalendar.MONTH)) {
cal_month.set((cal_month.get(GregorianCalendar.YEAR) - 1),
cal_month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
cal_month.set(GregorianCalendar.MONTH,
cal_month.get(GregorianCalendar.MONTH) - 1);
}
}
public void refreshCalendar() {
cal_adapter.refreshDays();
cal_adapter.notifyDataSetChanged();
tv_month.setText(android.text.format.DateFormat.format("MMMM yyyy", cal_month));
}
}
第二个片段
package com.idkstudios.shiftcalculator.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import com.idkstudios.shiftcalculator.R;
import com.idkstudios.shiftcalculator.calendar.adapter.AndroidListAdapter;
import com.idkstudios.shiftcalculator.calendar.util.CalendarCollection;
import java.util.ArrayList;
/**
* Created by Toby on 02/09/2015.
*/
public class HoursListFragment extends Fragment implements View.OnClickListener{
private ListView lv_android;
private AndroidListAdapter list_adapter;
private Button btn_calender;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hours, container, false);
CalendarCollection.date_collection_arr = new ArrayList<CalendarCollection>();
CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-01","John Birthday"));
CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-04","Client Meeting at 5 p.m."));
CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-06","A Small Party at my office"));
CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-05-02","Marriage Anniversary"));
CalendarCollection.date_collection_arr.add(new CalendarCollection("2015-04-11","Live Event and Concert of sonu"));
getWidget();
return rootView;
}
public void getWidget(){
btn_calender = (Button) getActivity().findViewById(R.id.btn_calender);
btn_calender.setOnClickListener(this);
lv_android = (ListView) getActivity().findViewById(R.id.lv_android);
list_adapter = new AndroidListAdapter(getActivity(), R.layout.list_item, CalendarCollection.date_collection_arr);
lv_android.setAdapter(list_adapter);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_calender:
startActivity(new Intent(getActivity(), CalendarFragment.class));
break;
default:
break;
}
}
}
最后我还没有实际编码的第三个,
package com.idkstudios.shiftcalculator.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.idkstudios.shiftcalculator.R;
/**
* Created by Toby on 02/09/2015.
*/
public class PayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pay, container, false);
return rootView;
}
}
如果您需要,这是Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idkstudios.shiftcalculator" >
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是主要活动的适配器类
package com.idkstudios.shiftcalculator.tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.idkstudios.shiftcalculator.fragments.CalendarFragment;
import com.idkstudios.shiftcalculator.fragments.HoursListFragment;
import com.idkstudios.shiftcalculator.fragments.PayFragment;
/**
* Created by Toby on 02/09/2015.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fragmentManager){
super(fragmentManager);
}
@Override
public Fragment getItem(int index) {
switch(index){
case 0:
return new HoursListFragment();
case 1:
return new CalendarFragment();
case 2:
return new PayFragment();
}
return null;
}
@Override
public int getCount() {
/**
* This value is equal to the number of tabs you have in the bar
*/
return 3;
}
}
我确实考虑过制定第二个计划。这三个片段都来自一个活动类,所以我将活动类中的代码复制并粘贴到片段中,这可能就是问题所在。我考虑过使用原始的acitivty类,然后从fragment类开始一个新的intent到activity类。但那么片段类的重点是什么呢。我不太确定这个想法是否会起作用。
任何帮助都会得到很多帮助。如果您需要更多代码,请告诉我,我很乐意提供它,无论是一些适配器还是xml代码。
谢谢托比。
答案 0 :(得分:3)
HoursListFragment.onClick()
有这个错误的陈述:
startActivity(new Intent(getActivity(), CalendarFragment.class));
如您所知,片段不是活动。您的意思是通过MainActivity.class
代替CalendarFragment.class
吗?
答案 1 :(得分:1)
我认为问题是你的viewpager适配器, 并且您的MainActivity可以更好地扩展FragmentActivity 因为你想使用片段
然后在MainActivity类中定义三个字段,如下所示
CalendarFragment calenderFragment = new CalendarFragment ();
HoursListFragment hourListFragment = new HoursListFragment ();
PayFragment payFragment = new PayFragment ();
和一个整数作为NUMBER_OF_VIEWPAGER_PAGES 或更短的变量名称,如NUM_PAGE
public static final int NUM_PAGES = 3;
并为您的视图寻呼机制作适配器,如此
private class MyAdapter extends FragmentStatePagerAdapter {
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return tabTitles[0] ;
case 1:
return tabTitles[1] ;
default:
return tabTitles[2] ;
}
}
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return calenderFragment;
case 1:
return hourListFragment;
case 2:
return payFragment;
default:
return null;
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
最后将适配器设置为viewPager
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));