Java 活动不可分配给活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21975302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Activity is not assignable to Activity
提问by hozdaman
I have read several posts for the last couple of days and i can not seem to figure out what my issue is. I have checked the Activity names and they are the same. I have also checked my manifest and I have the .
in front of the activities as well.
过去几天我阅读了几篇文章,但似乎无法弄清楚我的问题是什么。我检查了活动名称,它们是相同的。我还检查了我的清单,并且.
在活动前面也有。
Here is my logcat file:
这是我的 logcat 文件:
02-23 16:48:22.438 1508-1508/com.pctoolman.planme.app E/AndroidRuntime﹕ FATAL
EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.pctoolman.planme.app/com.pctoolman.planme.app.PlanMeMainActivity}:
java.lang.ClassCastException: com.pctoolman.planme.app.PlanMeMainActivity cannot be
cast
to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.pctoolman.planme.app.PlanMeMainActivity
cannot
be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access0(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pctoolman.planme.app" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.pctoolman.planme.app.PlanMeMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.pctoolman.planme.app.NewEventSetupActivity"
android:label="@string/setup">
</activity>
</application>
PlanMeMainFragment.java
PlanMeMainFragment.java
package com.pctoolman.planme.app;
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;
public class PlanMeMainFragment extends Fragment {
private Button mNewButton, mExistingButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.planme_main_fragment, parent, false);
mNewButton = (Button)v.findViewById(R.id.new_event_button);
mNewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
getActivity().startActivity(myIntent);
}
});
return v;
}
}
PlanMeMainActivity.java
PlanMeMainActivity.java
package com.pctoolman.planme.app;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class PlanMeMainActivity extends Fragment {
public Fragment createFragment() {
return new PlanMeMainFragment();
}
}
采纳答案by Yakiv Mospan
回答by Quamber Ali
Where are you wrong?
你哪里错了?
<activity
android:name="com.pctoolman.planme.app.PlanMeMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Why are you wrong?
你为什么错?
The mistake is that you added the Fragment in the Manifest as Activity. However Fragments are not activities
错误是你在 Manifest 中添加了 Fragment 作为 Activity。然而片段不是活动
What you should do?
你应该怎么做?
You should add the fragment in an activity and then define that activity in the Manifest
您应该在活动中添加片段,然后在清单中定义该活动
回答by chrylis -cautiouslyoptimistic-
Your PlanMeMainActivity
isn't an Activity
; it's a Fragment
. Your activity needs to be an activity, and you can then add fragments to it.
你PlanMeMainActivity
的不是Activity
; 这是一个Fragment
. 您的活动必须是活动,然后您可以向其中添加片段。
回答by Neo
Your PlanMainActivity extends a Fragment. A fragment cannot be cast to an activity
您的 PlanMainActivity 扩展了一个 Fragment。片段无法转换为活动
回答by Lalit Rane
Clean Project solved this error. The root cause of the error in my case was different, but I got the same "Activity is not assignable to Activity" error.
Clean Project 解决了这个错误。在我的情况下错误的根本原因是不同的,但我得到了相同的“活动不可分配给活动”错误。
I faced this problem after copying existing (perfectly working) Android Project to create a new one.
After copying the project, the AppCompatActivity was not recognized causing all the activities in the Android Manifest file to show the error "activity-is-not-assignable-to-activity".
Which was solved by cleaning the project.
在复制现有(完美运行)Android 项目以创建一个新项目后,我遇到了这个问题。复制项目后,无法识别 AppCompatActivity,导致 Android Manifest 文件中的所有活动都显示错误“activity-is-not-assignable-to-activity”。
这是通过清理项目解决的。
回答by Dante ache
Finally, I find out the solution is add following line in your dependencies:
最后,我发现解决方案是在您的依赖项中添加以下行:
compile 'com.android.support:support-v4:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
回答by hamidjahandideh
just delete ".gradle" folder of your project and press sync button . its solved my problem
只需删除项目的“.gradle”文件夹,然后按同步按钮。它解决了我的问题
回答by Vodyanikov Andrew Anatolevich
In my case the error was occurred by just installed plugin in Android Studio. So check your plugins and uninstall odd ones.
在我的情况下,错误是由刚刚在 Android Studio 中安装的插件发生的。所以检查你的插件并卸载奇怪的插件。
回答by Dimitri de Jesus
My problem was solved by Invalidating Caches and Restarting after having Cleaned the Project.
我的问题是通过使缓存无效并在清理项目后重新启动来解决的。