我正在开发一款Android应用。在我的应用程序中,我需要以对话框的形式启动活动。我通过在清单中设置对话框主题找到了一种方法。但是当我开始活动时,它给了我错误。
这是我在清单
中配置的方式 <activity android:name=".ReplyActivity"
android:label="Reply"
android:theme="@android:style/Theme.Holo.Dialog">
<action android:name="com.blog.waiyanhein.mmfashion.SettingsActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</activity>
这就是我在适配器
中启动活动的方法 private void openReplyActivity(int position)
{
Intent intent = new Intent(context, ReplyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(ReplyActivity.NOTI_ID_FIELD,values.get(position).getId());
intent.putExtra(ReplyActivity.REPLY_TO_FIELD,values.get(position).getUsername());
context.startActivity(intent);
}
这是对话活动的XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/reply_tv_to_title"
android:text="Reply to"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/reply_tv_reply_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/reply_tf_message"
android:layout_width="match_parent"
android:layout_height="120dp" />
<android.support.v7.widget.AppCompatButton
android:layout_below="@+id/reply_tf_message"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
android:textColor="@color/white"
android:text="Reply"
android:id="@+id/reply_btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
这是我在Logcat中得到的错误
06-15 07:17:08.092 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4b65648)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blog.waiyanhein.mmfashion.mmfashion/com.blog.waiyanhein.mmfashion.mmfashion.ReplyActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.mmfashion.ReplyActivity.onCreate(ReplyActivity.java:25)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5133)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-15 07:17:08.096 20542-20542/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我在Gradle中设置支持库,就像这样
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
我的代码出了什么问题?为什么我不能以代码对话框开始活动?
答案 0 :(得分:3)
<activity
android:name=".YourDialogActivityName"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:theme="@style/Theme.AppCompat.Light.Dialog"
>