我的程序在模拟器上不断崩溃

时间:2017-11-01 00:25:34

标签: java android

对不起,英语不好。 我是android studio和Java的新手。 这可能是基本的,但其他相同问题的答案都没有解决我的问题。 它表示我的主要活动和其他活动不是抽象方法,并且不会覆盖OnNavigationItemSelectedListener上的抽象方法。

所以我找到了一些通过将这些活动改为抽象或实现方法来解决问题的解决方案。但程序崩溃并在模拟器上初始化时停止。我找不到任何解决方案,我已经阅读了日志猫,无法弄清我代码的错误。

任何帮助?

这是我的主要活动:

package com.example.andik.inibudi;

import android.content.Intent;
import android.support.annotation.NonNull;
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.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Gallery;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toogle);
        toogle.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) {
        getMenuInflater().inflate(R.menu.nav_drawer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public boolean onNavigationsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.gallery) {
            Intent searchIntent = new Intent(MainActivity.this, com.example.andik.inibudi.Gallery.class);
            startActivity(searchIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        } else if (id == R.id.fileimport) {
            Intent searchIntent = new Intent(MainActivity.this, FileImport.class);
            startActivity(searchIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        } else if (id == R.id.slideshow) {
            Intent searchIntent = new Intent(MainActivity.this, SlideShow.class);
            startActivity(searchIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}

这是我的日志猫:

11-01 05:22:41.367 6008-6008/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.andik.inibudi, PID: 6008
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andik.inibudi/com.example.andik.inibudi.MainActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #2: Error inflating class RelativeLayoutLayout
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                     at android.os.Handler.dispatchMessage(Handler.java:105)
                                                     at android.os.Looper.loop(Looper.java:164)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                  Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #2: Error inflating class RelativeLayoutLayout
                                                  Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class RelativeLayoutLayout
                                                  Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RelativeLayoutLayout" on path: DexPathList[[zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/base.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.andik.inibudi-9Kf2k2xmP7LCW9vSmyH9Lg==/lib/x86, /system/lib, /system/vendor/lib]]
                                                     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
                                                     at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
                                                     at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
                                                     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)
                                                     at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
                                                     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                                                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                     at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)

1 个答案:

答案 0 :(得分:0)

这是因为您没有正确覆盖NavigationView.OnNavigationItemSelectedListener。您添加以下方法:

public boolean onNavigationsItemSelected(MenuItem item) {
}

但它应该是:

public boolean onNavigationItemSelected(MenuItem item) {
}

@ mike-m指出的另一个错误,你也有错误的布局视图:

RelativeLayoutLayout

应该是

RelativeLayout