我创建了一个弹出窗口。这在Activity中运行良好。 但是当我在片段中使用此弹出窗口时,会发生异常。 我无法理解这个问题。
ChangePasswordFragment.java
public class ChangePasswordFragment extends Fragment implements OnClickListener,AllInOneAsyncTask.ServiceResultListener {
private EditText changeCurrentPassword,changeNewPassword,retypeNewPassword;
private Button btnUpdate;
private View rootView;
private ProgressDialog progress;
private AlertDialog popuupDialog;
private Handler messageHandler;
private AppSharedPreference appSharedPreference;
// flag for Internet connection status
private Boolean isInternetPresent = false;
// Connection detector class
private ConnectionDetector cd;
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_change_password, container, false);
// creating connection detector class instance
cd = new ConnectionDetector(getActivity());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
messageHandler = new Handler();
appSharedPreference = AppSharedPreference.getInstance(getActivity());
initView(rootView);
} else {
// Internet connection is not present
// Ask user to connect to Internet
AppUtil.showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", false);
}
return rootView;
}
private void initView(View view)
{
changeCurrentPassword=(EditText)view.findViewById(R.id.change_current_password);
changeNewPassword=(EditText)view.findViewById(R.id.change_new_password);
retypeNewPassword=(EditText)view.findViewById(R.id.retype_new_password);
btnUpdate=(Button)view.findViewById(R.id.btn_update);
btnUpdate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_update:
// check for Internet status
if (isInternetPresent) {
progress = new ProgressDialog(getActivity());
progress.setMessage("Please Wait, It may take few seconds...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
messageHandler.postDelayed(new Runnable() {
public void run() {
checkValidation();
}
}, 3000);
} else {
// Internet connection is not present
// Ask user to connect to Internet
AppUtil.showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", false);
}
break;
}
}
@SuppressWarnings("unchecked")
private void callChangePasswordApi() {
String string_changeCurrentPassword = changeCurrentPassword.getText().toString();
String string_changeNewPassword = changeNewPassword.getText().toString();
Map<String, String> param = null;
try {
param = new HashMap<String, String>();
param.put("username", appSharedPreference.getUserName());
param.put("trpass", appSharedPreference.getPassword());
param.put("oldpassword", string_changeCurrentPassword);
param.put("newpassword", string_changeNewPassword);
param.put("action", "FORGOT_PASS");
}
catch(Exception e)
{
e.printStackTrace();
}
String jobjstr = param.toString();
Log.e("LOGINREQUESTTTTTTT", jobjstr);
if (jobjstr != null) {
AllInOneAsyncTask asyncTask = new AllInOneAsyncTask(getActivity());
asyncTask.setServiceResultListener(this);
asyncTask.setServiceType(Constants.SERVICE_TYPE_GET_PROFILE);
asyncTask.execute(param);
}
}
@SuppressWarnings("unused")
private void checkValidation()
{
String string_changeCurrentPassword = changeCurrentPassword.getText().toString();
String string_changeNewPassword = changeNewPassword.getText().toString();
String string_retypeNewPassword = retypeNewPassword.getText().toString();
if(!string_changeCurrentPassword.matches("") && string_changeCurrentPassword.matches(appSharedPreference.getPassword()))
{
if(string_changeNewPassword.length() >= 6 )
{
if(!string_changeNewPassword.matches(string_changeCurrentPassword))
{
if(string_retypeNewPassword.length() >= 6 && string_retypeNewPassword.matches(string_changeNewPassword))
{
callChangePasswordApi();
}
else
{
if (progress != null || progress.isShowing())
progress.dismiss();
showChangePasswordRechargePopup(getActivity(), getResources().getString(R.string.password_not_match));
}
}
else
{
if (progress != null || progress.isShowing())
progress.dismiss();
showChangePasswordRechargePopup(getActivity(), getResources().getString(R.string.password_match));
}
}
else
{
if (progress != null || progress.isShowing())
progress.dismiss();
showChangePasswordRechargePopup(getActivity(), getResources().getString(R.string.wrong_password));
}
}
else
{
if (progress != null || progress.isShowing())
progress.dismiss();
showChangePasswordRechargePopup(getActivity(), getResources().getString(R.string.correct_password));
}
}
@Override
public void onResult(String resultData, int requestType) {
// TODO Auto-generated method stub
if (requestType == Constants.SERVICE_TYPE_GET_PROFILE) {
try {
JSONObject obj = new JSONObject(resultData);
String status = obj.getString("Status");
String message = obj.getString("Message");
if (status.equals("1")) {
String string_changeNewPassword = changeNewPassword.getText().toString();
appSharedPreference.setPassword(string_changeNewPassword);
if (progress != null || progress.isShowing())
progress.dismiss();
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
startActivity(new Intent(getActivity(), MainActivity.class));
getActivity().finish();
}
else {
if (progress != null || progress.isShowing())
progress.dismiss();
showChangePasswordRechargePopup(getActivity(),message);
}
}
catch(Exception e)
{
}
}
}
public void showChangePasswordRechargePopup(final Context ctx, String message)
{
LayoutInflater li = LayoutInflater.from(ctx);
View promptsView = li.inflate(R.layout.popup, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);
alertDialogBuilder.setView(promptsView);
TextView tvMessage = (TextView)promptsView.findViewById(R.id.tvMessage);
Button btnOk = (Button)promptsView.findViewById(R.id.btnOk);
tvMessage.setText(message);
btnOk.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
popuupDialog.dismiss();
}
});
// create alert dialog
popuupDialog = alertDialogBuilder.create();
popuupDialog.getWindow().setBackgroundDrawableResource(R.drawable.popup_bg);
// show it
popuupDialog.show();
}
}
popup.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/popup_bg" >
<com.webzone.allbillpay.ui.CustomTextView
android:id="@+id/tvMessage"
app:typeface="Roboto-Regular.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="100dp"
android:textColor="@color/black"
android:textSize="18sp"/>
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:text="Ok"
android:textColor="@color/white"
android:background="@drawable/button_selector"
android:textAllCaps="false"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
异常
05-27 16:12:08.536 29591-29591/com.webzone.allbillpay E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.webzone.allbillpay, PID: 29591
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:48)
at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:92)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:882)
at com.webzone.allbillpay.fragment.ChangePasswordFragment.showChangePasswordRechargePopup(ChangePasswordFragment.java:242)
at com.webzone.allbillpay.fragment.ChangePasswordFragment.checkValidation(ChangePasswordFragment.java:192)
at com.webzone.allbillpay.fragment.ChangePasswordFragment.access$000(ChangePasswordFragment.java:33)
at com.webzone.allbillpay.fragment.ChangePasswordFragment$1.run(ChangePasswordFragment.java:109)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webzone.allbillpay"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="com.webzone.allbillpay.application.AllBillPayApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.webzone.allbillpay.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Dark">
</activity>
<activity
android:name="com.webzone.allbillpay.MainActivity"
android:label="@string/app_name">
</activity>
<activity
android:name="com.webzone.allbillpay.ForgetPasswordActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.Dark">
</activity>
<activity
android:name="com.webzone.allbillpay.ConfirmMobileRechargeActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.WithActionBar">
</activity>
<activity
android:name="com.webzone.allbillpay.ConfirmDTHRechargeActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.WithActionBar">
</activity>
<activity
android:name="com.webzone.allbillpay.ConfirmDatacardRechargeActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.WithActionBar">
</activity>
</application>
</manifest>
style.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Black">
<item name="android:textColorPrimary">@color/white</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:windowBackground">@color/primary</item>
<item name="colorControlNormal">@color/primary_darker</item>
<item name="colorControlActivated">@color/black</item>
<item name="colorControlHighlight">@color/black</item>
<item name="android:textColorHint">@color/iron</item>
<item name="colorButtonNormal">@color/primary_darker</item>
<!--<item name="android:colorButtonNormal">@color/primary_darker</item>-->
</style>
<style name="AppTheme.Dark.Dialog" parent="Theme.AppCompat.Dialog">
<item name="colorAccent">@color/white</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:background">@color/primary_darker</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="EditTextStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">10dp</item>
<item name="android:gravity">left</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:ems">10</item>
</style>
<style name="ButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:padding">10dp</item>
<item name="android:layout_gravity">center</item>
</style>
</resources>
如果任何人有解决方案,请提供帮助。
答案 0 :(得分:0)
更改主题
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
并添加menifest.xml