Ridiculous_
Ridiculous_
2015-12-18 12:18
采纳率: 100%
浏览 3.4k
已采纳

在eclipse里的Android程序为什么一运行就停止运行了?(新人求教)

package com.android.mytextview;

import java.lang.reflect.Field;

import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.LinkMovementMethod;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

public int getResourceId(String name) {
    try {
        Field feild = R.drawable.class.getField(name);
        return Integer.parseInt(feild.get(null).toString());
    } catch (Exception e) {
        // TODO: handle exception
    }

    return 0;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = (TextView) this.findViewById(R.id.textview3);
    textView.setTextColor(Color.BLACK);
    textView.setBackgroundColor(Color.WHITE);
    textView.setTextSize(20);
    String html = "艾斯<img src='image(1)'/>海贼<img src='image(2)'/>哆啦A梦<img src='image(3)'/><p>";
    html += "传送门<a href='http://baidu.com'><img src='image(6)'></a>功夫熊猫<img src='image(4)'/>";

    CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {

        @Override
        public Drawable getDrawable(String source) {
            // TODO 自动生成的方法存根
            Drawable drawable = getResources().getDrawable(
                    getResourceId(source));
            if (source.equals("image(3)")) {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2,
                        drawable.getIntrinsicHeight() / 2);
            } else {
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                        drawable.getIntrinsicHeight());
            }
            return drawable;
        }
    }, null);
    textView.setText(charSequence);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

}

这是错误日志
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (1).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (1).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (1).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (2).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (2).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (2).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (3).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (3).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (3).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (4).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (4).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (4).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (6).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (6).png: Invalid file name: must contain only [a-z0-9_.]
[2015-12-18 20:13:37 - android_textview1] res\drawable-hdpi-v4\image (6).png: Invalid file name: must contain only [a-z0-9_.]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • lxfeng1098751554
    笑笑风 2015-12-20 03:58
    已采纳

    android_textview1] res\drawable-hdpi-v4\image (1).png: Invalid file name: must contain only [a-z0-9_.],说明你的文件命名有问题,去掉()

    点赞 评论
  • bdmh
    bdmh 2015-12-18 12:24

    文件名字不对,去掉括号

    点赞 评论
  • bdmh
    bdmh 2015-12-18 12:26

    image(1)去掉括号

    点赞 评论
  • qq_33425116
    chriszht_163 2015-12-18 12:28

    res\drawable-hdpi-v4\image (1).png
    你的这个文件含有非法的文件名(Invalid file name),正确的文件名应该只含有[_a-z0-9_.]
    应该是复制粘贴后产生的吧

    点赞 评论
  • daidaineteasy
    晓呆同学 2015-12-18 12:40

    image (1).png image (2).png你在取图片的时候,这个图片的名字不对,你可以检察一下。

    Invalid file name: must contain only [_a-z0-9_.]
    这句话的意思是,有效的文件名,必须只包含中括弧、点、下划线、短线、字母a到z以及数字等内容。

    点赞 评论
  • hehui1860
    小小豆豆浆 2015-12-20 03:40

    错误日志说的很明确啊 png图片命名问题

    点赞 评论

相关推荐