2015-05-20 65 views
1

我有一個可變的webColor,如果按下該按鈕,我想填充顏色代碼,因此下一頁(subHeader)上的imageView將與按鈕。基於android中的按鈕單擊來填充bg顏色

我得到這些錯誤

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

該應用是否崩潰,當您單擊按鈕或當你啓動應用程序? –

回答

2

我認爲你有#c41e72更換"@string/webColor"在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="#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)); 


}} 

+0

oooh非常感謝你,我想我現在完全瞭解你,謝謝你!所以你必須用滿足字符串需求的值來啓動它,並且通過它來覆蓋它? – dreday

+0

我得到這個問題,因爲它無法找到我們在content_activity活動中引用的意圖.C:\ Users \ Andreas \ Downloads \ dredaycreativeportfolio \ app \ src \ main \ java \ com \ dredaydesigns \ dredaycreativeportfolio \ content_activity.java 錯誤:(17,34)錯誤:找不到符號變量意圖 注意:某些輸入文件使用或覆蓋棄用的API。 注意:使用-Xlint:deprecation重新編譯以獲取詳細信息。 錯誤:執行任務':app:compileDebugJava'失敗。 >編譯失敗;詳細信息請參閱編譯器錯誤輸出。 ' – dreday

+0

oooh得到它的工作!添加了意向的前面!謝謝你幫助我!嗬! – dreday

相關問題