应用停止工作

时间:2014-01-19 16:07:39

标签: android textview logcat justify

首先我正在尝试this way。我被困了然后我问问题但是没有人给出任何答案。然后我尝试this link中@ sayed.jalil建议的其他方式。但我的应用程序停止工作。如果有人提供第一个链接的答案真的appriciated。如果有人喜欢回答替代方案,我已经按照@ sayed.jalil回答了。对于我的代码 -

Xml代码 -

<com.info.abc.JustifiedTextView  
android:id="@+id/textview1"
android:textColor="#FFFFFF"/> 

活动代码 -

JustifiedTextView txtViewEx = (JustifiedTextView) findViewById(R.id.textview1);
txtViewEx.setText("some text");

JustifiedTextView.java -

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class JustifiedTextView extends WebView {

    private String core = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 10px 10px 10px 10px;'>%s</body></html>";
    private String textColor = "0,0,0,255";
    private String text = "";
    private int textSize = 12;
    private int backgroundColor = Color.TRANSPARENT;

    public JustifiedTextView(Context context) {
        super(context);
        this.setWebChromeClient(new WebChromeClient() {

        });

    }

    public void setText(String s) {
        this.text = s;
        // this.setPadding(10, 10, 10, 10);
        reloadData();
    }

    @SuppressLint("NewApi")
    private void reloadData() {

        // loadData(...) has a bug showing utf-8 correctly. That's why we need
        // to set it first.
        this.getSettings().setDefaultTextEncodingName("utf-8");

        this.loadData(String.format(core, textColor, textSize, text),
            "text/html", "utf-8");

        // set WebView's background color *after* data was loaded.
        super.setBackgroundColor(backgroundColor);
        // Hardware rendering breaks background color to work as expected.
        // Need to use software renderer in that case.
        if (android.os.Build.VERSION.SDK_INT >= 11)
            this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }

    public void setTextColor(int hex) {
        String h = Integer.toHexString(hex);
        int a = Integer.parseInt(h.substring(0, 2), 16);
        int r = Integer.parseInt(h.substring(2, 4), 16);
        int g = Integer.parseInt(h.substring(4, 6), 16);
        int b = Integer.parseInt(h.substring(6, 8), 16);
        textColor = String.format("%d,%d,%d,%d", r, g, b, a);
        reloadData();
    }

    public void setBackgroundColor(int hex) {
        backgroundColor = hex;
        reloadData();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        reloadData();
    }

}

的logcat -

01-19 10:43:19.400: E/AndroidRuntime(1482): FATAL EXCEPTION: main
01-19 10:43:19.400: E/AndroidRuntime(1482): Process: com.info.abc, PID: 1482
01-19 10:43:19.400: E/AndroidRuntime(1482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.info.abc/com.info.abc.History}: 
android.view.InflateException: Binary XML file line #24: Error inflating class com.info.abc.JustifiedTextView
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.os.Looper.loop(Looper.java:137)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at java.lang.reflect.Method.invoke(Method.java:515)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at dalvik.system.NativeStart.main(Native Method)
01-19 10:43:19.400: E/AndroidRuntime(1482): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class com.info.abc.JustifiedTextView
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.createView(LayoutInflater.java:603)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.Activity.setContentView(Activity.java:1928)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at com.info.abc.History.onCreate(History.java:19)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.Activity.performCreate(Activity.java:5243)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-19 10:43:19.400: E/AndroidRuntime(1482):     ... 11 more
01-19 10:43:19.400: E/AndroidRuntime(1482): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
01-19 10:43:19.400: E/AndroidRuntime(1482):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at java.lang.Class.getConstructor(Class.java:446)
01-19 10:43:19.400: E/AndroidRuntime(1482):     at android.view.LayoutInflater.createView(LayoutInflater.java:568)
01-19 10:43:19.400: E/AndroidRuntime(1482):     ... 23 more

1 个答案:

答案 0 :(得分:3)

Caused by: java.lang.NoSuchMethodException: 
<init> [class android.content.Context, interface android.util.AttributeSet]
01-19 10:43:19.400: E/AndroidRuntime(1482):
     at java.lang.Class.getConstructorOrMethod(Class.java:472)

您未提供自定义视图所需的所有构造函数 具体来说,添加此构造函数。阅读有关如何使用属性集的文档和教程:

public JustifiedTextView(Context context, AttributeSet attrs){   
    super(context, attrs);
    this.setWebChromeClient(new WebChromeClient() {
    });
}