尝试使用Intent跳转至新Activity时程序停止运行
 package com.level1.yukinoshita.swan0;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.level1.yukinoshita.fragment.Fragment_autocharge;
import com.level1.yukinoshita.fragment.Fragment_replyaction;
import com.level1.yukinoshita.fragment.Fragment_searchall;
import com.level1.yukinoshita.fragment.Fragment_socialaction;
import com.level1.yukinoshita.fragment.Fragment_socialuser;
import com.level1.yukinoshita.fragment.Fragment_userfavor;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

//    private RecyclerView contentRecyclerView;
//    private List<String> recycleViewData;
//    private RecycleViewSimpleAdapter contentRecyclerViewAdapter;
private static final int STACK=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Snackbar.make(view, "正在刷新", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_socialuser) {
//            分配对应Fragment
            Fragment_socialuser fragment = new Fragment_socialuser();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_socialaction) {
            Fragment_socialaction fragment = new Fragment_socialaction();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_autocharge) {
            Fragment_autocharge fragment = new Fragment_autocharge();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_replyaction) {
            Fragment_replyaction fragment = new Fragment_replyaction();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_userfavor) {
            Fragment_userfavor fragment = new Fragment_userfavor();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_searchall) {
            Fragment_searchall fragment = new Fragment_searchall();
            getFragmentManager().beginTransaction().replace(R.id.main_contentview, fragment).commit();
        } else if (id == R.id.nav_settings) {
//            使用Activity
            Intent intent = new Intent(this, Activity_settings.class);
            startActivity(intent);
        } else if (id == R.id.nav_developer) {
//            使用Activity
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

报错信息如下

04-15 01:34:22.769 6114-6114/com.level1.yukinoshita.swan0 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.level1.yukinoshita.swan0, PID: 6114
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.level1.yukinoshita.swan0/com.level1.yukinoshita.swan0.Activity_settings}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.level1.yukinoshita.swan0.MainActivity.onNavigationItemSelected(MainActivity.java:125)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:151)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-15 01:34:24.908 6114-6114/com.level1.yukinoshita.swan0 I/Process: Sending signal. PID: 6114 SIG: 9

5个回答

你应该是没有在 AndroidManifest.xml文件中声明要跳转的Activity,所以跳转失败。

你是不是没有在 AndroidManifest.xml文件中注册要跳转的Activity,四大组件都得再清单文件中注册。

如果注册过后还报错的话,Intent intent = new Intent(this, Activity_settings.class);
startActivity(intent);在this前加上mainactivity.
这样写Intent intent = new Intent(MainActivity.this, Activity_settings.class);
startActivity(intent);

没有在清单文件中找到你要跳转的界面

把别的文件代码发来我运行一下。。。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问