从字符串分析的错误颜色 [英] Error parsing Colors from String

查看:281
本文介绍了从字符串分析的错误颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:一个由引擎收录我的项目的相关部分的:

这里是更新code

此外ColouredItem是一个包装:

 公共类ColouredItem
     {//只有一个包装类,无行为已在此定义
        字符串名称,颜色;
     }

尝试使用以下code解析一个字符串的颜色,当我得到一个NumberFormatException异常:

  row.setBackgroundColor(Color.parseColor(item.colour));

我用下面从资源创建的项目清单:

 的for(int i = 0; I< List.length的数字,我++)
    {
        项目=新ColouredMenuItem();
        的String [] c菜单=列表[我] .split(#);
        item.name = c菜单[0];
        item.colour =#+ c菜单[1];
        Log.d(TAG,item.colour);
        menuList.add(项目);
    }

这是我得到的异常......我发现,view.setBackgroundColor只需要一个整数值:

  #FFFFFF
         #ffffBB
         #fff45f
         #FFFF00
         关闭VM
         主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
         致命异常:主要
             java.lang.NumberFormatException:FFFFFF
         在java.lang.Long.parse(Long.java:364)
         在java.lang.Long.parseLong(Long.java:354)
         在android.graphics.Color.parseColor(Color.java:207)
         在com.example.samplelistproject.MadAdapter.getView(MadAdapter.java:60)
         在android.widget.AbsListView.obtainView(AbsListView.java:1315)
         在android.widget.ListView.makeAndAddView(ListView.java:1727)
         在android.widget.ListView.fillDown(ListView.java:652)
         在android.widget.ListView.fillFromTop(ListView.java:709)
         在android.widget.ListView.layoutChildren(ListView.java:1580)
         在android.widget.AbsListView.onLayout(AbsListView.java:1147)
         在android.view.View.layout(View.java:7035)
         在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         在android.view.View.layout(View.java:7035)
         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
         在android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
         在android.view.View.layout(View.java:7035)
         在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         在android.view.View.layout(View.java:7035)
         在android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
         在android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
         在android.os.Handler.dispatchMessage(Handler.java:99)
         在android.os.Looper.loop(Looper.java:123)
         在android.app.ActivityThread.main(ActivityThread.java:4627)
         在java.lang.reflect.Method.invokeNative(本机方法)
         在java.lang.reflect.Method.invoke(Method.java:521)
         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
         在dalvik.system.NativeStart.main(本机方法)

添加#为一些答案建议并没有解决这个问题:

  java.lang.NumberFormatException:无效长:#FFFFFF
      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
      在android.os.Handler.dispatchMessage(Handler.java:99)
      在android.os.Looper.loop(Looper.java:137)
      在android.app.ActivityThread.main(ActivityThread.java:5103)
      在java.lang.reflect.Method.invokeNative(本机方法)

本实施无差异之一:

 字符串c菜单=列表[我]
          item.name = cmenu.substring(0,cmenu.indexOf(#));
          item.colour = cmenu.substring(cmenu.indexOf(#));


解决方案

使用此code

row.setBackgroundColor(Color.parseColor(#424242));

它帮助我太多,不要删除#。

我用这个code

 私人列表<串GT;项目;项目=新的ArrayList<串GT;();
item.add(#424242);
row.setBackgroundColor(Color.parseColor(item.get(0)));

及其工作GUD对我来说,可能是你的事情分裂不工作好

或你的code

 按钮BTN;
ColouredMenuItem项目;
ArrayList的< ColouredMenuItem>菜单列表=新的ArrayList< ColouredMenuItem>();
的String [] =名单新的String [] {第1页#FFFFFF,第2页#ffffBB};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);    的setContentView(R.layout.sample);    尝试{
        BTN =(按钮)findViewById(R.id.button1);
        的for(int i = 0; I< List.length的数字,我++){
            项目=新ColouredMenuItem();
            的String [] c菜单=列表[我] .split(#);
            item.name = c菜单[0];
            item.color =#+ c菜单[1];
            Log.d(五色,item.color);
            menuList.add(项目);
        }        btn.setBackgroundColor(Color.parseColor(menu​​List.get(1)。颜色));
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
}

在我身边这是工作好

这是新的code

请您有色项目作为一个bean类的getter和setter这样

 公共类ColouredMenuItem {//只有一个包装类,无行为已被定义
                        // 这里
字符串名称,颜色;清单< ColouredMenuItem>名单=新的ArrayList< ColouredMenuItem>();公开名单< ColouredMenuItem>的GetList(){
    返回列表;
}公共无效SETLIST(列表< ColouredMenuItem>菜单列表){
    this.list =菜单列表;
}公共字符串的getName(){
    返回名称;
}公共无效setname可以(字符串名称){
    this.name =名称;
}公共字符串getColour(){
    返回的颜色;
}公共无效setColour(串色){
    this.colour =颜色;
}

}

然后在你的适配器使用code

  {尝试
        Log.d(TAG,menuList.get(位置).colour);
        textView.setText(menu​​List.get(位置).getName());        {
            row.setBackgroundColor(Color.parseColor(menu​​List.get(位置).getColour()));
        }
    }赶上(例外前){
        Log.e(TAG,还不行);
    }

只要给它一个尝试,它在这里工作在我身边。

此外,您的数组是这样才呐

 <字符串数组名=menu_array>
    <项目>页1#ff7788< /项目>
    <项目>页1#ff6688< /项目>
    <项目>第1页#424242< /项目>
< /字符串数组>

EDIT:A Pastebin consisting of the relevant parts of my project:

Here is the updated code

Also ColouredItem is a wrapper for:

     public class ColouredItem
     {//Only a wrapper class,no behaviour has been defined here
        String name,colour;
     }

I get a NumberFormatException when trying to parse a colour from a String using the following code:

     row.setBackgroundColor(Color.parseColor(item.colour));

I use the following to create a list of items from a resource:

    for(int i=0;i<list.length;i++)
    {
        item=new ColouredMenuItem();
        String[] cmenu =list[i].split("#");
        item.name=cmenu[0];
        item.colour="#"+cmenu[1];
        Log.d(TAG, item.colour);
        menuList.add(item);
    }

This is the exception that I get...I have found that view.setBackgroundColor only takes an integer value:

         #ffffff 
         #ffffBB 
         #fff45f 
         #ffff00 
         Shutting down VM
         threadid=1: thread exiting with uncaught exception (group=0x4001d800)
         FATAL EXCEPTION: main
             java.lang.NumberFormatException: ffffff 
         at java.lang.Long.parse(Long.java:364)
         at java.lang.Long.parseLong(Long.java:354)
         at android.graphics.Color.parseColor(Color.java:207)
         at com.example.samplelistproject.MadAdapter.getView(MadAdapter.java:60)
         at android.widget.AbsListView.obtainView(AbsListView.java:1315)
         at android.widget.ListView.makeAndAddView(ListView.java:1727)
         at android.widget.ListView.fillDown(ListView.java:652)
         at android.widget.ListView.fillFromTop(ListView.java:709)
         at android.widget.ListView.layoutChildren(ListView.java:1580)
         at android.widget.AbsListView.onLayout(AbsListView.java:1147)
         at android.view.View.layout(View.java:7035)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         at android.view.View.layout(View.java:7035)
         at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
         at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
         at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
         at android.view.View.layout(View.java:7035)
         at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
         at android.view.View.layout(View.java:7035)
         at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
         at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:123)
         at android.app.ActivityThread.main(ActivityThread.java:4627)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:521)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
         at dalvik.system.NativeStart.main(Native Method)

Adding the # as some of the answers suggest did not solve the issue:

          java.lang.NumberFormatException: Invalid long: "#ffffff"
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      at android.app.ActivityThread.access$600(ActivityThread.java:141)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5103)
      at java.lang.reflect.Method.invokeNative(Native Method)

No difference with this implementation either:

          String cmenu=list[i];
          item.name=cmenu.substring(0, cmenu.indexOf("#"));
          item.colour=cmenu.substring(cmenu.indexOf("#"));

解决方案

Use this code

row.setBackgroundColor(Color.parseColor("#424242"));

it helped me too,dont remove "#".

i used this code

private List<String> item;

item = new ArrayList<String>();
item.add("#424242");
row.setBackgroundColor(Color.parseColor(item.get(0)));

and its working gud for me,may be your split thing is not working good

or for your code

Button btn;
ColouredMenuItem item;
ArrayList<ColouredMenuItem> menuList = new ArrayList<ColouredMenuItem>();
String[] list = new String[] { "Page1 #ffffff", "Page2 #ffffBB" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.sample);

    try {
        btn = (Button) findViewById(R.id.button1);
        for (int i = 0; i < list.length; i++) {
            item = new ColouredMenuItem();
            String[] cmenu = list[i].split("#");
            item.name = cmenu[0];
            item.color = "#" + cmenu[1];
            Log.d("colored", item.color);
            menuList.add(item);
        }



        btn.setBackgroundColor(Color.parseColor(menuList.get(1).color));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

this is working good at my side

this is new code

Make your colored item as a bean class with getter and setter like this

public class ColouredMenuItem {// Only a wrapper class,no behaviour has been defined
                        // here
String name, colour;

List<ColouredMenuItem> list=new ArrayList<ColouredMenuItem>();

public List<ColouredMenuItem> getList() {
    return list;
}

public void setList(List<ColouredMenuItem> menuList) {
    this.list = menuList;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

}

Then in your adapter use this code

try {
        Log.d(TAG, menuList.get(position).colour);
        textView.setText(menuList.get(position).getName());

        {
            row.setBackgroundColor(Color.parseColor(menuList.get(position).getColour()));
        }
    } catch (Exception ex) {
        Log.e(TAG, "Still does not work");
    }

Just give it a try,it works here at my side

Also your array is like this only na

<string-array name="menu_array">
    <item>Page1 #ff7788</item>
    <item>Page1 #ff6688</item>
    <item>Page1 #424242</item>
</string-array>

这篇关于从字符串分析的错误颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆