应用程序停止工作 [英] App stop working
问题描述
首先我想<一个href=\"http://stackoverflow.com/questions/21202560/justify-text-in-textview-not-working-properly\">this方式。而我坚持我接着问问题,但是,没有人给出任何回答。然后,我尝试在由@ sayed.jalil建议此链接另一种方式。但是,我的应用程序停止工作。如果有人在第一个环节提供答案真的appriciated。如果有人想回答的替代,我根据@ sayed.jalil答案尝试。对于我的code以下 -
XML code -
&LT; com.info.abc.JustifiedTextView
机器人:ID =@ + ID / textview1
机器人:文字颜色=#FFFFFF/&GT;
活动code -
JustifiedTextView txtViewEx =(JustifiedTextView)findViewById(R.id.textview1);
txtViewEx.setText(一些文字);
JustifiedTextView.java -
进口android.annotation.Sup pressLint;
进口android.content.Context;
进口android.graphics.Color;
进口android.text.SpannableString;
进口android.webkit.WebChromeClient;
进口android.webkit.WebView;
公共类JustifiedTextView扩展的WebView { 私人字符串核心=&LT; HTML&GT;&LT;车身风格=文本对齐:理由;颜色:RGBA(%S); FONT-SIZE:%DPX;保证金:10px的10px的10px的10px的;'&GT;%S&LT; /身体GT;&LT; / HTML&gt;中;
私人字符串文字颜色=0,0,0,255;
私人字符串文本=;
私人INT TEXTSIZE = 12;
私人诠释的backgroundColor = Color.TRANSPARENT; 公共JustifiedTextView(上下文的背景下){
超级(上下文);
this.setWebChromeClient(新WebChromeClient(){ }); } 公共无效的setText(String s)将{
this.text =秒;
// this.setPadding(10,10,10,10);
reloadData();
} @燮pressLint(NewApi)
私人无效reloadData(){ // loadData(...)具有显示正确的UTF-8的错误。这就是为什么我们需要
//先进行设定。
this.getSettings()setDefaultTextEncodingName(UTF-8); this.loadData(的String.format(核心,文字颜色,TEXTSIZE,文字)
text / html的,UTF-8); //设置的WebView背景色* *后的数据被加载。
super.setBackgroundColor(的backgroundColor);
//硬件渲染休息背景色为达到预期效果。
//需要使用软件渲染在这种情况下。
如果(android.os.Build.VERSION.SDK_INT&GT; = 11)
this.setLayerType(WebView.LAYER_TYPE_SOFTWARE,NULL);
} 公共无效setTextColor(INT十六进制){
串H = Integer.toHexString(十六进制);
诠释一个=的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);
文字颜色=的String.format(%D,%D,%D,%D,R,G,B,A);
reloadData();
} 公共无效setBackgroundColor(INT十六进制){
的backgroundColor =十六进制;
reloadData();
} 公共无效setTextSize(INT TEXTSIZE){
this.textSize = TEXTSIZE;
reloadData();
}}
logcat的 -
10月1日至19日:43:19.400:E / AndroidRuntime(1482):致命异常:主要
十月1日至19日:43:19.400:E / AndroidRuntime(1482):工艺:com.info.abc,PID:1482
十月1日至19日:43:19.400:E / AndroidRuntime(1482):了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.info.abc/com.info.abc.History:
android.view.InflateException:二进制XML文件行#24:错误充气类com.info.abc.JustifiedTextView
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread.access $ 700(ActivityThread.java:135)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1397)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.os.Handler.dispatchMessage(Handler.java:102)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.os.Looper.loop(Looper.java:137)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread.main(ActivityThread.java:4998)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在java.lang.reflect.Method.invokeNative(本机方法)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在java.lang.reflect.Method.invoke(Method.java:515)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在dalvik.system.NativeStart.main(本机方法)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):android.view.InflateException:二进制XML文件行#24:错误充气类com.info.abc.JustifiedTextView产生的原因
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.createView(LayoutInflater.java:603)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.inflate(LayoutInflater.java:492)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.inflate(LayoutInflater.java:397)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.inflate(LayoutInflater.java:353)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.Activity.setContentView(Activity.java:1928)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在com.info.abc.History.onCreate(History.java:19)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.Activity.performCreate(Activity.java:5243)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):11 ...更多
十月1日至19日:43:19.400:E / AndroidRuntime(1482):java.lang.NoSuchMethodException:产生的原因&LT;&初始化GT; [类android.content.Context,接口android.util.AttributeSet]
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在java.lang.Class.getConstructorOrMethod(Class.java:472)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在java.lang.Class.getConstructor(Class.java:446)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在android.view.LayoutInflater.createView(LayoutInflater.java:568)
十月1日至19日:43:19.400:E / AndroidRuntime(1482):23 ...更多
产生的原因:java.lang.NoSuchMethodException:其中,初始化&GT; [类android.content.Context,接口android.util.AttributeSet]
十月1日至19日:43:19.400:E / AndroidRuntime(1482):在java.lang.Class.getConstructorOrMethod(Class.java:472)
您没有提供所有的自定义视图所需要的构造函数。具体来说,加上此构造函数。阅读如何使用属性集的文档和教程:
公共JustifiedTextView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
this.setWebChromeClient(新WebChromeClient(){
});
}
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
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() {
});
}
这篇关于应用程序停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!