问题
I've one problem: I use SugarORM for my app and in AndroidManifest I need to set this:
<meta-data android:name="DATABASE" android:value="moneyManager.db" />
<meta-data android:name="VERSION" android:value="1"/>
<meta-data android:name="QUERY_LOG" android:value="true"/>
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="ua.marinovskiy.moneymanager" />
And this:
android:name="com.orm.SugarApp"
And now, when I'm using startActivityForResult from adapter:
Intent intent = new Intent(context, NewOperation.class);
((Activity) context).startActivityForResult(intent, MainFragment.REQUEST_CODE);
I take this error:
java.lang.ClassCastException: com.orm.SugarApp cannot be cast to android.app.Activity
My question: how I can cut this android:name="com.orm.SugarApp" from Manifest file or another way to resolve this problem?
EDIT:
public class MainFragment extends Fragment {
public final static int REQUEST_CODE = 1;
ViewPager viewPager;
PagerAdapter viewPagerAdapter;
List<List<CategoryParent>> list;
List<Integer> balance_list = new ArrayList<>();
List<String> periods_list = new ArrayList<>();
SugarHelper sugarHelper;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,
container, false);
sugarHelper = new SugarHelper();
list = sugarHelper.readAllOoerations(MainActivity.type_of_period, MainActivity.min_date, MainActivity.max_date, MainActivity.addedDate, balance_list);
periods_list = sugarHelper.getPeriods();
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPagerAdapter = new ViewPagerAdapter(getActivity().getApplicationContext(), list, balance_list, periods_list);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(MainActivity.addedDate);
return view;
}
@Override
public void onResume() {
super.onResume();
list.clear();
list.addAll(sugarHelper.readAllOoerations(MainActivity.type_of_period, MainActivity.min_date, MainActivity.max_date, MainActivity.addedDate, balance_list));
if (!list.isEmpty()) {
viewPagerAdapter.notifyDataSetChanged();
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(MainActivity.addedDate);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
Toast.makeText(getActivity().getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
}
}
}
And my ViewPagerAdapter class:
public class ViewPagerAdapter extends PagerAdapter {
private OperationsAdapter operationsAdapter;
private List<CategoryParent> categoryParentArrayList;
private ExpandableListView expandableListView;
TextView tv_balance, empty, current_period;
Context context;
List<List<CategoryParent>> list;
List<Integer> balance_list;
List<String> periods;
LayoutInflater inflater;
SugarHelper sugarHelper;
public ViewPagerAdapter(Context context, List<List<CategoryParent>> list, List<Integer> balance_list, List<String> periods) {
this.context = context;
this.list = list;
this.balance_list = balance_list;
this.periods = periods;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewpager_item, container,
false);
sugarHelper = new SugarHelper();
tv_balance = (TextView) view.findViewById(R.id.balance_operations);
current_period = (TextView) view.findViewById(R.id.current_period);
current_period.setText(periods.get(position));
setBalance(balance_list, position);
expandableListView = (ExpandableListView) view.findViewById(R.id.expListView);
categoryParentArrayList = list.get(position);
if (!categoryParentArrayList.isEmpty()) {
empty = (TextView) view.findViewById(R.id.if_empty);
empty.setVisibility(View.INVISIBLE);
operationsAdapter = new OperationsAdapter(context, categoryParentArrayList);
expandableListView.setAdapter(operationsAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
v.setBackgroundColor(Color.parseColor("#B2EBF2"));
Intent intent = new Intent(context, NewOperation.class);
MainFragment mainFragment = new MainFragment();
mainFragment.startActivityForResult(intent, MainFragment.REQUEST_CODE);
return false;
}
});
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(context, "group", Toast.LENGTH_LONG).show();
return false;
}
});
}
((ViewPager) container).addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
public void setBalance(List<Integer> balance_list, int position) {
tv_balance.setText("Balance: $" + balance_list.get(position));
if (balance_list.get(position) < 0) {
tv_balance.setBackgroundResource(R.drawable.balance_minus_style);
} else {
tv_balance.setBackgroundResource(R.drawable.balance_plus_style);
}
}
And exception:
09-03 14:40:03.915 13091-13091/ua.marinovskiy.moneymanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment MainFragment{426ee278} not attached to Activity
at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:906)
at ua.marinovskiy.moneymanager.adapter.ViewPagerAdapter$1.onChildClick(ViewPagerAdapter.java:95)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:583)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
at android.widget.AbsListView$1.run(AbsListView.java:3571)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
ua.marinovskiy.moneymanager.adapter.ViewPagerAdapter$1.onChildClick(ViewPagerAdapter.java:95)
at mainFragment.startActivityForResult(intent, MainFragment.REQUEST_CODE);
SOLUTION:
I leave
((Activity) context).startActivityForResult(intent, MainFragment.REQUEST_CODE);
and change in fragment, when I call adapter getActivity().getApplicationContext() on getActivity().
回答1:
You MUST specify com.orm.SugarApp
in your manifest, there is no avoiding this.
You are trying to cast context
to an Activity in this line:
((Activity) context).startActivityForResult(intent, MainFragment.REQUEST_CODE);
context
is actually an instance of com.orm.SugarApp
hence why you are getting the crash.
Since you are executing this from a Fragment then use the following code instead:
getActivity().startActivityForResult(intent, MainFragment.REQUEST_CODE);
UPDATE:
Don't pass an Application context to your adapter, pass an Activity context:
This is wrong:
viewPagerAdapter = new ViewPagerAdapter(getActivity().getApplicationContext(), list, balance_list, periods_list);
viewPager.setAdapter(viewPagerAdapter);
Do this instead:
viewPagerAdapter = new ViewPagerAdapter(getActivity(), list, balance_list, periods_list);
viewPager.setAdapter(viewPagerAdapter);
UPDATE:
You're instantiating a Fragment, not attaching it to anything and then trying to use it to fire off a startActivitiesForResult intent. This is wrong, use the context you already have within the adapter for this:
Change this:
MainFragment mainFragment = new MainFragment();
mainFragment.startActivityForResult(intent, MainFragment.REQUEST_CODE);
To this:
((Activity) context).startActivityForResult(intent, MainFragment.REQUEST_CODE);
来源:https://stackoverflow.com/questions/32354916/sugarorm-in-androidmanifest