(BEGINNER) 调用 setContentView 时出错?

Posted

技术标签:

【中文标题】(BEGINNER) 调用 setContentView 时出错?【英文标题】:(BEGINNER) Error when calling setContentView? 【发布时间】:2018-06-28 04:32:47 【问题描述】:

总的来说,我是编码的初学者,因此非常感谢您提供任何帮助。在活动中调用 setContentView 时,我收到“无法开始活动”。顺便说一句,我正在使用 CardView,没有足够的字符来放置所有其他布局。

AboutActivity.java

package com.example.adend.timetable.activities;

import android.content.Intent;
import com.example.adend.timetable.appwidgets.AbsThemeActivity;
import com.example.adend.timetable.R;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.kabouzeid.appthemehelper.ThemeStore;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AboutActivity extends AbsThemeActivity 

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setBackgroundColor(ThemeStore.primaryColor(this));
        setStatusbarColor(ThemeStore.primaryColor(this));

    

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 

            case R.id.action_settings:

                Intent intentS = new Intent(AboutActivity.this, SettingsActivity.class);
                AboutActivity.this.startActivity(intentS);
                return true;

            default:

                return super.onOptionsItemSelected(item);

        
    


activity__about.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical"
    tools:context="

activities.AboutActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/Toolbar"
        android:background="@android:color/transparent" />

    <ScrollView
        android:layout_
        android:layout_
        android:clipToPadding="false"
        android:isScrollContainer="true">

        <include layout="@layout/activity_about_content" />

    </ScrollView>

</LinearLayout>

activity_about_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:padding="8dp">

    <include
        layout="@layout/card_about_app"
        android:layout_
        android:layout_
        android:layout_marginBottom="8dp" />

    <include
        layout="@layout/card_author"
        android:layout_
        android:layout_
        android:layout_marginBottom="8dp" />

</LinearLayout>

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Light">

        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".activities.SubjectSelectorActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Light"
            android:parentActivityName="com.example.adend.timetable.activities.MainActivity">
        </activity>

        <activity
            android:name=".activities.SettingsActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Light"
            android:parentActivityName="com.example.adend.timetable.activities.MainActivity">
        </activity>

        <activity
            android:name=".activities.AboutActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Light"
            android:parentActivityName="com.example.adend.timetable.activities.MainActivity">
        </activity>

    </application>

</manifest>

Logcat

01-18 19:17:05.371 3755-3755/com.example.adend.timetable E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.adend.timetable, PID: 3755
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.adend.timetable/com.example.adend.timetable.activities.AboutActivity: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                               at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                               at android.os.Looper.loop(Looper.java:164)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                            Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
                                                                            Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                               at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
                                                                               at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                                                                               at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                               at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                                                                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                                               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.parseInclude(LayoutInflater.java:995)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
                                                                               at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                               at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                                               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:287)
                                                                               at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                               at com.example.adend.timetable.activities.AboutActivity.onCreate(AboutActivity.java:22)
                                                                               at android.app.Activity.performCreate(Activity.java:6999)
                                                                               at android.app.Activity.performCreate(Activity.java:6990)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                               at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                               at android.os.Looper.loop(Looper.java:164)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
01-18 19:17:05.372 3755-3755/com.example.adend.timetable E/AndroidRuntime: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValuet=0x2/d=0x7f04019e a=-1
                                                                               at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:944)
                                                                               at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
                                                                               at android.view.View.<init>(View.java:4768)
                                                                               at android.view.ViewGroup.<init>(ViewGroup.java:597)
                                                                               at android.widget.LinearLayout.<init>(LinearLayout.java:234)
                                                                               at android.widget.LinearLayout.<init>(LinearLayout.java:230)
                                                                               at android.widget.LinearLayout.<init>(LinearLayout.java:226)


                                                        ... 38 more

【问题讨论】:

你能在这里评论来自 logcat 的另外 38 个吗? @jace 抱歉,我该怎么做? 尝试调试您的代码并找出您的代码在哪一行遇到问题。 堆栈跟踪显示实例化 LinearLayout 存在问题,因为它的两个属性包括布局中的下层 - card_about_appcard_author 【参考方案1】:

事实证明,这只是styles.xml中没有提到的一个属性

【讨论】:

以上是关于(BEGINNER) 调用 setContentView 时出错?的主要内容,如果未能解决你的问题,请参考以下文章

翻译:Lisp Style Tips for the Beginner - Heinrich Taube

如何将 Google 标记设置为按钮?

Atcoder Beginner Contest 251 D——题解

Atcoder Beginner Contest 257

UnityLearn_Beginner_UnityBasics

AtCoder Beginner Contest 234