无法在AndroidManifest.xml中添加Intent

得意犹堪夸世俗,诏黄新湿字如鸦。这篇文章主要讲述无法在AndroidManifest.xml中添加Intent相关的知识,希望能为你提供帮助。
我想将意图添加到导航抽屉的不同值,但我收到一个错误

java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.tanis.myapplication / com.example.tanis.myapplication.MenuItems.Home}:java.lang.ClassCastException:com.example.tanis.myapplication.MenuItems.Home无法转换为android.app.Activity
请帮忙
Manifest
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tanis.myapplication"> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="WebTechiq" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".MainActivity"> < /activity> < activity android:name=".Splash" android:label="WebTechniQ"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < activity android:name=".MenuItems.Registration" android:label="Registration" android:theme="@style/AppTheme.NoActionBar"> < intent-filter> < action android:name="android.intent.action.SEND" /> < category android:name="android.intent.category.DEFAULT" /> < /intent-filter> < /activity> < activity android:name=".MenuItems.Home" android:label="Home" android:theme="@style/AppTheme.NoActionBar"> < intent-filter> < action android:name="android.intent.action.SEND" /> < category android:name="android.intent.category.DEFAULT" /> < /intent-filter> < /activity> < /application> < /manifest>

Home.java
package com.example.tanis.myapplication.MenuItems; /** * Created by tanis on 27-06-2018. */import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.tanis.myapplication.Adapters.CardItemString; import com.example.tanis.myapplication.Adapters.CardPagerAdapterS; import com.example.tanis.myapplication.Adapters.ViewPagerAdapter; import com.example.tanis.myapplication.R; import com.example.tanis.myapplication.Services.website_designing; import java.util.Timer; import java.util.TimerTask; public class Home extends Fragment {ViewPager mViewPager; CardPagerAdapterS mCardAdapter; ShadowTransformer mCardShadowTransformer; ViewPager viewPager; String titlesText[] = {" Website Design", " Digital Marketing", " Domain Registration", "Graphics Design", " Mobile Apps", " Server Hosting", " Software Development", " Content Marketing", " Security (SSl)"}; String detailsArray[] = { "Your website is your digital home. We create, design, redesign, develop, improvise, and implement. We make beautiful websites", "We help your business reach potential customers on every possible digital device through all possible media channels", "Tolaunchyourwebsitethefirstthingyouneedisthedomainname.Youcanchooseyourdomainnamewithushere", "Wegeneratecreativesolutionsandcancreateawiderangeofgraphicforyourclientswhichmatchtheirbusiness", "We are mobile. And we make you mobile. We make responsive websites and mobile apps which compliment your business", "WhenyouarehostingyourwebsiteintheIndiayouwillbenefitfromahigherpingrateandlowestlatency", "Our team is competent at coding web apps with keen attention to detail & intuitive functionality that is high on design & creativity", "Contentistheheartofyourdigitalpresence. Wecreatethe rightcontentwiththerightfocusforyourbusiness", "Secure your site with the world's leadingprovider of online security and getthese exclusive features at no addedcost", }; int[] images = {R.drawable.website_design, R.drawable.digita, R.drawable.domain_registration, R.drawable.gric, R.drawable.mob, R.drawable.server, R.drawable.software_development, R.drawable.ontent, R.drawable.ssl}; private Context context; public Home() { // Required empty public constructor }/** * Use this factory method to create a new instance of * this fragment. * * @return A new instance of fragment FragmentAction. */@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); }@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View v = inflater.inflate(R.layout.home, container, false); TextView txt = (TextView) v.findViewById(R.id.textView12); txt.setText("u25BA Creative & Dedicated Team"); TextView txt1 = (TextView) v.findViewById(R.id.textView13); txt1.setText("u25BA Affordable Cost"); TextView txt2 = (TextView) v.findViewById(R.id.textView14); txt2.setText("u25BA Maintain Long Relationship"); TextView txt3 = (TextView) v.findViewById(R.id.textView15); txt3.setText("u25BA Timely Deliverly "); context = this.getContext(); Button b = (Button) v.findViewById(R.id.button7); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), website_designing.class); } }); mViewPager = (ViewPager) v.findViewById(R.id.viewpager1); mCardAdapter = new CardPagerAdapterS(); for (int i = 0; i < titlesText.length; i++) {mCardAdapter.addCardItemS(new CardItemString(titlesText[i], detailsArray[i], images[i])); }mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter); mViewPager.setAdapter(mCardAdapter); mViewPager.setPageTransformer(false, mCardShadowTransformer); mViewPager.setOffscreenPageLimit(3); viewPager = (ViewPager) v.findViewById(R.id.viewpager); ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this.getContext()); viewPager.setAdapter(viewPagerAdapter); Timer timer = new Timer(); timer.scheduleAtFixedRate(new Mytime(), 2000, 4000); FloatingActionButton floatingActionButton = (FloatingActionButton) v.findViewById(R.id.floatingActionButton); floatingActionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(getActivity(), Registration.class)); } }); return v; }@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); }public class Mytime extends TimerTask { @Override public void run() {getActivity().runOnUiThread(new Runnable() { @Override public void run() {if (viewPager.getCurrentItem() == 0) { viewPager.setCurrentItem(1); } else if (viewPager.getCurrentItem() == 1) { viewPager.setCurrentItem(2); } else if (viewPager.getCurrentItem() == 2) { viewPager.setCurrentItem(3); } else if (viewPager.getCurrentItem() == 3) { viewPager.setCurrentItem(4); } else { viewPager.setCurrentItem(0); } } }); } } }

答案由于以下行,您收到错误:
< activity android:name=".MenuItems.Home" android:label="Home" android:theme="@style/AppTheme.NoActionBar"> < intent-filter> < action android:name="android.intent.action.SEND" /> < category android:name="android.intent.category.DEFAULT" /> < /intent-filter> < /activity>

你不能在Home中添加一个片段(你的Fragment类扩展AndroidManifest.xml)。你需要一个Activity来加载这个片段。关注official guide正确使用Fragment。
另一答案
没有片段可以通过Manifest实例化。你只能使用AppCompatActivity进行扩展。没有必要在Manifest中实例化片段
为此你应该改变
public class Home extends AppCompatActivity {.....}

现在Manifest永远不会显示任何错误,如果你想在事件发生时调用片段,你应该使用另一种方法
如果你想显示一个片段,下面的代码将帮助你
主要活动
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {Button bd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bd=(Button) findViewById(R.id.submit); bd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); BlankFragment fragmentFeaturedNews = new BlankFragment(); fragmentTransaction.replace(R.id.framelayout, fragmentFeaturedNews); fragmentTransaction.commit(); } }); } }

【无法在AndroidManifest.xml中添加Intent】主要的xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.stacktest.MainActivity" android:orientation="vertical"> < RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> < Button android:id="@+id/submit" android:layout_width="150dp" android:layout_height="60dp" android:text="fetch Fragments" android:layout_centerHorizontal="true"/> < FrameLayout android:id="@+id/framelayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/submit" android:layout_marginTop="80dp"> < /FrameLayout> < /RelativeLayout> < /LinearLayout>


    推荐阅读