从String解析颜色时出错

vam*_*olu 5 android colors numberformatexception

编辑:由我的项目的相关部分组成的Pastebin:

这是更新的代码

ColouredItem也是一个包装器:

     public class ColouredItem
     {//Only a wrapper class,no behaviour has been defined here
        String name,colour;
     }
Run Code Online (Sandbox Code Playgroud)

尝试使用以下代码从String解析颜色时,我得到NumberFormatException:

     row.setBackgroundColor(Color.parseColor(item.colour));
Run Code Online (Sandbox Code Playgroud)

我使用以下内容从资源创建项目列表:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

这是我得到的例外...我发现view.setBackgroundColor只接受一个整数值:

         #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)
Run Code Online (Sandbox Code Playgroud)

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

          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)
Run Code Online (Sandbox Code Playgroud)

与此实现没有区别:

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

Mee*_*nal 10

使用此代码

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

它也帮助了我,不要删除"#".

我用这个代码

private List<String> item;

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

对我来说,它的工作对手,可能是你分手的事情并不好

或者你的代码

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这对我很有帮助

这是新代码

使用这样的getter和setter将您的彩色项目作为bean类

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;
}
Run Code Online (Sandbox Code Playgroud)

}

然后在您的适配器中使用此代码

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");
    }
Run Code Online (Sandbox Code Playgroud)

试试吧,它在我这边工作

你的阵列也是这样的,只有na

<string-array name="menu_array">
    <item>Page1 #ff7788</item>
    <item>Page1 #ff6688</item>
    <item>Page1 #424242</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)