Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

android - App stop working

First I am trying this way. And I am stuck then I ask question But no one gives any answer. Then I try another way suggested by @sayed.jalil in this link. But my app stop working. If someone provide answer for first link Really appriciated. And if someone like to answer the alternative I've tried according to @sayed.jalil answer. For that my code below-

Xml code-

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

Activity code-

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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)

You are not providing all of the constructors required for a custom view.
Specifically, add this constructor. Read the documentation and tutorials for how to use the attribute set:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...