(初学者)调用 setContentView 时出错?
(BEGINNER) Error when calling setContentView?
总的来说,我是编码的新手,因此非常感谢任何对此的帮助。我在 activity 中调用 setContentView
时收到 'Unable to start activity'。顺便说一句,我正在使用 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_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<include
layout="@layout/card_about_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
<include
layout="@layout/card_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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 ComponentInfo{com.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: TypedValue{t=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
事实证明,这只是 styles.xml
中未提及的属性
总的来说,我是编码的新手,因此非常感谢任何对此的帮助。我在 activity 中调用 setContentView
时收到 'Unable to start activity'。顺便说一句,我正在使用 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_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<include
layout="@layout/card_about_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp" />
<include
layout="@layout/card_author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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 ComponentInfo{com.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: TypedValue{t=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
事实证明,这只是 styles.xml
中未提及的属性