以下是我的客户类,它扩展了PagerAdapter:
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
private Vector<View> pages;
private static activity2 act2;
public CustomPagerAdapter(Context context, Vector<View> pages) {
this.act2 = new activity2();
this.mContext=context;
this.pages=pages;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
View page = pages.get(position);
page.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Log.i("TAG", "This page was clicked: " + position);
act2.receiptAction(position, mContext);
return false;
}
});
container.addView(page);
return page;
}
}
我的Activity2的代码(需要从CustomPagerAdapter调用)
public void receiptAction(final Integer CurrentPage, final Context tContext){
if (CurrentPage > 0) {
final ApplicationInfo thContext = tContext.getApplicationInfo();
final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(tContext.getApplicationContext());
dlgAlert.setTitle("Receipt");
dlgAlert.setMessage(receiptPool.get(CurrentPage));
dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//();
//startActivity(lastcreated.getIntent()); //REFRESH INTENT
}
});
if (!receiptPool.get(CurrentPage).contains("_(Canceled)")) {
dlgAlert.setNeutralButton("Void", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dlgAlert.create().cancel();
final AlertDialog.Builder dlgAlert2 = new AlertDialog.Builder(tContext.getApplicationContext());
dlgAlert2.setTitle("Void ?");
dlgAlert2.setMessage(receiptPool.get(CurrentPage));
dlgAlert2.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
cancelOrder(receiptPool.get(CurrentPage), dlgAlert2);
}
});
dlgAlert2.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//();
//startActivity(lastcreated.getIntent()); //REFRESH INTENT
}
});
dlgAlert2.setCancelable(true);
dlgAlert2.create().show();
}
});
}
dlgAlert.setNegativeButton("Rebet", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
MainActivity.currentReceipt = receiptPool.get(CurrentPage);
MainActivity.isRebet = true;
intent = new Intent(tContext.getApplicationContext(), activity1.class);
startActivityForResult(intent, 0);
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
但是,我收到了这个错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
at android.support.v7.app.AlertController.installContent(AlertController.java:213)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240)
at android.app.Dialog.dispatchOnCreate(Dialog.java:463)
at android.app.Dialog.show(Dialog.java:288)
at com.M28.activity2.receiptAction(activity2.java:633)
at com.xsys.xobj.CustomPagerAdapter$1.onTouch(CustomPagerAdapter.java:37)
at android.view.View.dispatchTouchEvent(View.java:8956)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2698)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2410)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5303)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2371)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2559)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1767)
at android.app.Activity.dispatchTouchEvent(Activity.java:2866)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2520)
at android.view.View.dispatchPointerEvent(View.java:9167)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4706)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4544)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4201)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4258)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl.deliverInputEvent(ViewRoot
如何从CustomPagerAdapter调用Activity2中的函数?
答案 0 :(得分:0)
这个public void receiptAction(最终的Integer CurrentPage,最终的Context tContext)方法改为那样
public static void receiptAction(final Integer CurrentPage, final Context tContext)
然后从CustomPagerAdapter调用,如此
Activity2.receiptAction(current_page_number, mContext);
答案 1 :(得分:0)
android:theme="@style/AppTheme.NoActionBar"
在Activity2的manifest中使用上面的代码。
答案 2 :(得分:0)
请检查appcompat库并在gradle文件中支持库,您可能没有包含这些库,即
dependencies
{
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
}
答案 3 :(得分:0)
最后我得到了解决方案,查看Faisal Shaikh给出的答案(Dialog in Custom Adapter in android)