【问题标题】:App getting close when calling an activity from drawer从抽屉中调用活动时应用程序关闭
【发布时间】:2018-10-30 13:36:27
【问题描述】:

我是安卓开发新手 我的应用程序在调用特定活动时崩溃。(“GovernanaceActivity”是活动的名称)当我从 MainActivity 调用 Governance 活动时

这是主活动

package com.iridh.collegeapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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 android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static long back_pressed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Button more1=(Button)findViewById(R.id.btn_more);
        more1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent(MainActivity.this,GovernanceActivity.class);
                startActivity(intent);
            }
        });

        Button more2=(Button)findViewById(R.id.btn_more2);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", 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.addDrawerListener(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();
        Intent showIntent = new Intent();

        if (id == R.id.nav_home) {
            // Handle the camera action
        } else if (id == R.id.nav_governance) {


>  //problem getting here

            showIntent = new Intent(MainActivity.this, GovernanceActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_Campus) {
            showIntent = new Intent(MainActivity.this, CampusActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_department) {
            showIntent = new Intent(MainActivity.this, DepartmentsActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_admission) {
            showIntent = new Intent(MainActivity.this, AdmissionActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_facilities) {
            showIntent = new Intent(MainActivity.this, FacilitiesActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_gallery) {
            showIntent = new Intent(MainActivity.this, GalleryActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_placement) {
            showIntent = new Intent(MainActivity.this, PlacementActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        } else if (id == R.id.nav_contact) {
            showIntent = new Intent(MainActivity.this, ContactActivity.class);
            startActivity(showIntent);
            overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);

        }

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

}

这是正在调用的 GovernanceActivity

package com.iridh.collegeapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class GovernanceActivity extends AppCompatActivity {

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

**所有其他活动都运行良好,此活动的唯一问题

这是我的清单**

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iridh.collegeapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>




















                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".GovernanceActivity"
            android:parentActivityName=".MainActivity" />

        <activity android:name=".CampusActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".DepartmentsActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".AdmissionActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".FacilitiesActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".GalleryActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".DownloadActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".PlacementActivity"
            android:parentActivityName=".MainActivity"/>
        <activity android:name=".ContactActivity"
            android:parentActivityName=".MainActivity">

        </activity>
    </application>

</manifest>

我在下面附上日志

块引用 05-21 03:09:53.747 1823-1823/com.iridh.collegeapp D/AndroidRuntime: 关闭虚拟机 05-21 03:09:53.752 1823-1823/com.iridh.collegeapp E/AndroidRuntime: 致命异常: main 进程:com.iridh.collegeapp,PID:1823 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.iridh.collegeapp/com.iridh.collegeapp.GovernanceActivity}:android.view.InflateException:二进制 XML 文件第 288 行:二进制 XML 文件第 288 行:膨胀类错误文本视图 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 在 android.app.ActivityThread.-wrap12(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 在 android.os.Handler.dispatchMessage(Handler.java:108) 在 android.os.Looper.loop(Looper.java:166) 在 android.app.ActivityThread.main(ActivityThread.java:7425) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 原因:android.view.InflateException:二进制 XML 文件行#288:二进制 XML 文件行#288:膨胀类 TextView 时出错 引起:android.view.InflateException:二进制 XML 文件第 288 行:膨胀类 TextView 时出错 原因:android.content.res.Resources$NotFoundException:来自可绘制资源 ID #0x7f08005c 的文件 res/drawable/black.jpg:需要 .xml 扩展名 在 android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1371) 在 android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:1190) 在 android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1271) 在 android.content.res.Resources.loadColorStateList(Resources.java:1180) 在 android.content.res.HwResources.loadColorStateList(HwResources.java:289) 在 android.content.res.TypedArray.getColorStateList(TypedArray.java:572) 在 android.widget.TextView.(TextView.java:1291) 在 android.widget.TextView.(TextView.java:872) 在 android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:76) 在 android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:72) 在 android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:176) 在 android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101) 在 android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035) 在 android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:874) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:877) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:877) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:877) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:515) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:423) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:374) 在 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 在 com.iridh.collegeapp.GovernanceActivity.onCreate(GovernanceActivity.java:11) 在 android.app.Activity.performCreate(Activity.java:7372) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 在 android.app.ActivityThread.-wrap12(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 在 android.os.Handler.dispatchMessage(Handler.java:108) 在 android.os.Looper.loop(Looper.java:166) 在 android.app.ActivityThread.main(ActivityThread.java:7425) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

那里发生了什么我无法调试它 提前致谢。

这是我的 GovernanceActivity 附加布局 活动治理

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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">
    <android.support.v7.widget.LinearLayoutCompat
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:id="@+id/governance_lyt"
        android:background="@drawable/ic_menu_header_background">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:elevation="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp">

            <RelativeLayout
                android:id="@+id/title_lyt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FF941020">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:gravity="center"
                    android:textColor="#FFFFFF"
                    android:text="GOVERNANACE"
                    android:textSize="25dp"
                    android:textStyle="bold"
                    app:fontFamily="@font/android"
                    android:layout_alignParentLeft="true" />
            </RelativeLayout>


        </android.support.v7.widget.CardView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="     "
                android:textSize="15dp" />

        </RelativeLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:paddingTop="20dp"
            android:background="@android:color/darker_gray">

        </View>

        <android.support.v7.widget.CardView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:elevation="12dp"
            android:shape="ring"
            android:innerRadius="0dp"
            android:layout_gravity="center"
            android:thicknessRatio="1.9"
            android:layout_centerHorizontal="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_president" />
            </RelativeLayout>



        </android.support.v7.widget.CardView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Shree Shankarprasad Agnihotri"
                android:textSize="22dp"
                android:textStyle="bold"
                android:textColor="#263238"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="President"
                android:textSize="17dp"
                android:textStyle="bold"
                android:textColor="#666666"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="     "
                android:textSize="15dp" />

        </RelativeLayout>


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:elevation="100dp">

            <RelativeLayout
                android:id="@+id/title_lyt3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00e676">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:gravity="center"
                    android:text="Message"
                    android:textColor="#000000"
                    android:textSize="20dp"
                    android:textStyle="bold"
                    app:fontFamily="@font/android"
                    android:layout_alignParentLeft="true" />
            </RelativeLayout>




        </android.support.v7.widget.CardView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="349dp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="@string/president"
                android:textSize="17dp" />
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:paddingTop="20dp"
            android:background="@android:color/darker_gray">

        </View>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="     "/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="     "/>

        </RelativeLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:paddingTop="20dp"
            android:background="@android:color/darker_gray">

        </View>

        <android.support.v7.widget.CardView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:elevation="12dp"
            android:shape="ring"
            android:innerRadius="0dp"
            android:layout_gravity="center"
            android:thicknessRatio="1.9"
            android:layout_centerHorizontal="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_secretary" />
            </RelativeLayout>



        </android.support.v7.widget.CardView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Sachin S. Agnihotri"
                android:textSize="22dp"
                android:textStyle="bold"
                android:textColor="#263238"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Secretary"
                android:textSize="17dp"
                android:textStyle="bold"
                android:textColor="#666666"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="     "
                android:textSize="15dp" />

        </RelativeLayout>


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:elevation="100dp"
            >

            <RelativeLayout
                android:id="@+id/title_lyt2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background=" #00e676">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:gravity="center"
                    android:text="Message"
                    android:textSize="20dp"
                    android:textColor="@drawable/black"
                    android:textStyle="bold"
                    app:fontFamily="@font/android"
                    android:layout_alignParentLeft="true" />
            </RelativeLayout>




        </android.support.v7.widget.CardView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="349dp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="@string/secretary"
                android:textSize="17dp" />
        </RelativeLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:paddingTop="20dp"
            android:background="@android:color/darker_gray">

        </View>
    </android.support.v7.widget.LinearLayoutCompat>

</ScrollView>

【问题讨论】:

  • 它只是在你的R.layout.activity_governance 中崩溃,你能发布布局吗?
  • 从您的日志中: 引起:android.view.InflateException:二进制 XML 文件第 288 行:错误膨胀类 TextView 引起:android.content.res.Resources$NotFoundException:文件 res/drawable/ black.jpg 来自可绘制资源 ID #0x7f08005c:需要 .xml 扩展名(即找不到资源)。
  • @MarcEstrada 已结帐!
  • 感谢您的回归 Chetan。很高兴它奏效了。我想念那些声誉点... :)

标签: android android-activity


【解决方案1】:

您必须将文件 black.jpg 重命名为 black.xml,假设这是一个 XML 资源(如形状或选择器),或者如果它是图像,则重命名为 black.png

那是你的错误:

错误膨胀类 TextView 导致:android.view.InflateException:二进制 XML 文件行 #288:错误膨胀类 TextView 导致:android.content.res.Resources$NotFoundException:文件 res/drawable/black.jpg 来自 drawable资源 ID #0x7f08005c.xml 需要扩展

编辑:

这是你的错误:

           <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:gravity="center"
                android:text="Message"
                android:textSize="20dp"
                android:textColor="@drawable/black" <-- ERROR HERE
                android:textStyle="bold"
                app:fontFamily="@font/android"
                android:layout_alignParentLeft="true" />

您在textColor 中设置drawable,应该是color 资源。可以是@color/black(黑色应该在colors.xml 中定义为&lt;color name="black"&gt;#000000&lt;/color&gt; 或者可以是文字(android:textColor="#000000")。

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多