根据android

时间:2015-05-20 03:00:13

标签: android

我有一个变量webColor,如果按下该按钮,我想填充颜色代码,因此下一页上的imageView(subHeader)将与按钮颜色相同。

我收到这些错误

05-19 22:54:15.725    2050-2050/com.dredaydesigns.dredaycreativeportfolio E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.dredaydesigns.dredaycreativeportfolio, PID: 2050
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dredaydesigns.dredaycreativeportfolio/com.dredaydesigns.dredaycreativeportfolio.content_activity}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:620)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.dredaydesigns.dredaycreativeportfolio.content_activity.onCreate(content_activity.java:13)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.dredaydesigns.dredaycreativeportfolio.content_activity.onCreate(content_activity.java:13)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f090051}
            at android.content.res.Resources.loadDrawable(Resources.java:2068)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
            at android.view.View.<init>(View.java:3554)
            at android.widget.ImageView.<init>(ImageView.java:123)
            at android.widget.ImageView.<init>(ImageView.java:119)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.dredaydesigns.dredaycreativeportfolio.content_activity.onCreate(content_activity.java:13)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

这是我的代码

package com.dredaydesigns.dredaycreativeportfolio;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;



public class MainActivity extends ActionBarActivity {
    public String webColor;
    private Button mWebButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       mWebButton = (Button) findViewById(R.id.webButton);

        mWebButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String webColor = mWebButton.getText().toString();
                Intent goToNext = new Intent(MainActivity.this,content_activity.class);
                goToNext.putExtra(webColor,"#C41E72");
                startActivity(goToNext);

            }
        });


    }}

content_activity.java

package com.dredaydesigns.dredaycreativeportfolio;

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

import static com.dredaydesigns.dredaycreativeportfolio.R.layout.activity_content_activity;

public class content_activity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_content_activity);



}}

activity_content_activity.xml

<RelativeLayout 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"
                tools:context="com.dredaydesigns.dredaycreativeportfolio.content_activity"
                android:background="#0e6fa2">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pageBanner"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/webactivityheader02"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/subBanner"
        android:layout_below="@+id/pageBanner"
        android:layout_alignLeft="@+id/pageBanner"
        android:layout_alignStart="@+id/pageBanner"
        android:adjustViewBounds="false"
        android:padding="8dp"
        android:background="@string/webColor"/>
</RelativeLayout>

activity_main.xml中

<RelativeLayout 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"
                tools:context=".MainActivity"
                android:background="@drawable/mainactivity01"
                android:clickable="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/dredaylogo"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/webButton"
        android:layout_alignStart="@+id/webButton"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webButton"
        android:alpha="100"
        android:clickable="true"
        android:background="@drawable/web"
        android:layout_below="@+id/imageView"
        android:layout_alignLeft="@+id/interactiveButton"
        android:layout_alignStart="@+id/interactiveButton"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/interactiveButton"
        android:clickable="true"
        android:alpha="200"
        android:layout_below="@+id/webButton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/interactive"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mobileButton"
        android:alpha="100"
        android:clickable="true"
        android:background="@drawable/mobile"
        android:layout_below="@+id/interactiveButton"
        android:layout_alignLeft="@+id/interactiveButton"
        android:layout_alignStart="@+id/interactiveButton"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/creativeButton"
        android:alpha="100"
        android:clickable="true"
        android:background="@drawable/creative"
        android:layout_below="@+id/mobileButton"
        android:layout_alignLeft="@+id/mobileButton"
        android:layout_alignStart="@+id/mobileButton"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/strategyButton"
        android:alpha="100"
        android:clickable="true"
        android:background="@drawable/strategy"
        android:layout_below="@+id/creativeButton"
        android:layout_alignLeft="@+id/creativeButton"
        android:layout_alignStart="@+id/creativeButton"
        android:layout_gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/contentButton"
        android:alpha="100"
        android:clickable="true"
        android:background="@drawable/content"
        android:layout_below="@+id/strategyButton"
        android:layout_alignLeft="@+id/strategyButton"
        android:layout_alignStart="@+id/strategyButton"
        android:layout_gravity="center"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:layout_alignTop="@+id/imageView"
        android:layout_toLeftOf="@+id/imageView"
        android:layout_toStartOf="@+id/imageView"
        android:background="@drawable/mainactivity05"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView3"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"
        android:background="@drawable/mainactivity04"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

我认为您必须将activity_content_activity xml中的"@string/webColor"替换为#c41e72,如下所示

 <RelativeLayout 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"
            tools:context="com.dredaydesigns.dredaycreativeportfolio.content_activity"
            android:background="#0e6fa2">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pageBanner"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/webactivityheader02"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/subBanner"
    android:layout_below="@+id/pageBanner"
    android:layout_alignLeft="@+id/pageBanner"
    android:layout_alignStart="@+id/pageBanner"
    android:adjustViewBounds="false"
    android:padding="8dp"
    android:background="#c41e72"/>

如果要以编程方式设置,请编写以下代码

MainActivity中的

确实以下面的方式传递你的颜色

public class MainActivity extends ActionBarActivity {
public String webColor= "webColor";
private Button mWebButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   mWebButton = (Button) findViewById(R.id.webButton);

    mWebButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // String webColor = mWebButton.getText().toString();
            Intent goToNext = new Intent(MainActivity.this,content_activity.class);
            goToNext.putExtra(webColor,"#C41E72");
            startActivity(goToNext);

        }
    });


}}

并在content_activity活动中

 public class content_activity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_content_activity);
    String backgroundColor = intent.getExtras().getString("webColor");
    ImageView iv = (ImageView)findViewById(R.id.subBanner);
    iv.setBackgroundColor(Color.parseColor(backgroundColor));


}}