打开空白布局使应用程序崩溃

时间:2016-06-01 12:33:08

标签: java android android-studio

我正在创建一个应用并添加了一个新的资源布局文件。在Android Studio Preview中,我可以预览该文件。问题是,在调用布局时,应用程序正在崩溃。

details_main_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>

DetailsMain.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class DetailsMain extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_main);
    }
}

我在导航栏菜单项中调用此活动。代码如下。

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

        if (id == R.id.nav_details) {
        Intent in = new Intent(getApplicationContext(),DetailsMain.class);
            startActivity(in);

        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

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

我犯了什么错误?

logcat的

  

E / AndroidRuntime:致命异常:主要                                                                                   进程:switchmode.new.switch,PID:3156                                                                                   android.content.ActivityNotFoundException:无法找到显式   活动课   {switchmode.new.switch / switchmode.new.switch.DetailsMain};有你   在AndroidManifest.xml中声明了此活动?                                                                                       在   android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)                                                                                       在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)                                                                                       在android.app.Activity.startActivityForResult(Activity.java:3745)                                                                                       在android.app.Activity.startActivityForResult(Activity.java:3706)                                                                                       在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)                                                                                       在android.app.Activity.startActivity(Activity.java:4016)                                                                                       在android.app.Activity.startActivity(Activity.java:3984)                                                                                       在   switchmode.new.switch.MainActivity.onNavigationItemSelected(MainActivity.java:97)                                                                                       在   android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:151)                                                                                       在   android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)                                                                                       在   android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)                                                                                       在   android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)                                                                                       在   android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:318)                                                                                       在android.view.View.performClick(View.java:4789)                                                                                       在android.view.View $ PerformClick.run(View.java:19881)                                                                                       在android.os.Handler.handleCallback(Handler.java:739)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                       在android.os.Looper.loop(Looper.java:135)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5289)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在java.lang.reflect.Method.invoke(Method.java:372)                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

3 个答案:

答案 0 :(得分:1)

您忘记在清单文件中声明您的活动。

  <activity android:name="your.pakage.name.DetailsMain" />

答案 1 :(得分:0)

您需要在DetailsMain文件中声明Manifest

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity 
       android:name="com.example.DetailsMain"/>

</application>

答案 2 :(得分:0)

您尚未在清单文件中声明活动

<activity android:name="you_package_name.DetailsMain" />