问题
I'm going to have a justified textview
with Persian language and most of the this Android TextView Justify Text link solution wasn't really helpful except one of it's solution that was this link https://github.com/navabi/JustifiedTextView.
i implement the code like this but Unable to start activity ComponentInfo and it's says that Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView need to be inflated
but how can i inflate this.
this is my code:
MainActivity:
package com.example.android.justify;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private JustifiedTextView mJTv;
private TextView mTv;
private SeekBar mSbSize;
private LinearLayout mLlGreen, mLlRed;
private TextView mTvEng, mTvFa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mJTv = (JustifiedTextView) findViewById(R.id.activity_main_jtv);
mTv = (TextView) findViewById(R.id.activity_main_tv);
mLlGreen = (LinearLayout) findViewById(R.id.activity_main_ll_green);
mLlRed = (LinearLayout) findViewById(R.id.activity_main_ll_red);
mTvEng = (TextView) findViewById(R.id.activity_main_tv_eng);
mTvFa = (TextView) findViewById(R.id.activity_main_tv_fa);
mSbSize = (SeekBar) findViewById(R.id.activity_main_sb_size);
mSbSize.setMax(20);
mSbSize.setProgress(10);
/* mSbSize.setOnSeekBarChangeListener(this);
mLlGreen.setOnClickListener(this);
mLlRed.setOnClickListener(this);
mTvEng.setOnClickListener(this);
mTvFa.setOnClickListener(this);*/
}
public void onProgressChanged(SeekBar arg0, int pos, boolean arg2) {
int size = 10;
mTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, pos + size);
mJTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, pos + size);
mJTv.setLineSpacing(pos - size);
mTv.setLineSpacing(pos - size, 1);
}
}
and i added JustifiedTextView as a class and also XmlToClassAttribHandler
class that he also said to add it is here:
package com.example.android.justify;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
public class XmlToClassAttribHandler {
private Resources mRes;
private Context mContext;
private AttributeSet mAttributeSet;
private String namespace="http://noghteh.ir";
private final String KEY_TEXT="text";
private final String KEY_TEXT_SIZE="textSize";
private final String KEY_TEXT_COLOR="textColor";
public XmlToClassAttribHandler(Context context,AttributeSet attributeSet){
mContext=context;
mRes=mContext.getResources();
mAttributeSet=attributeSet;
}
public String getTextValue(){
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT);
if (value==null)
return "";
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@string/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
value=mRes.getString(resId);
}
return value;
}
public int getColorValue(){
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_COLOR);
int color= Color.BLACK;
if (value==null)
return color;
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@color/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
color=mRes.getColor(resId);
return color;
}
try{
color=Color.parseColor(value);
}
catch(Exception e){
return Color.BLACK;
}
return color;
}
public int getTextSize() {
int textSize=12;
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );
if (value==null)
return textSize;
if (value.length()>1 &&
value.charAt(0)=='@' &&
value.contains("@dimen/")){
int resId=mRes.getIdentifier(mContext.getPackageName()+":"+value.substring(1), null,null);
textSize=mRes.getDimensionPixelSize(resId);
return textSize;
}
try{
textSize=Integer.parseInt(value.substring(0, value.length()-2));
}
catch(Exception e){
return 12;
}
return textSize;
}
public int gettextSizeUnit() {
String value=mAttributeSet.getAttributeValue(namespace, KEY_TEXT_SIZE );
if (value==null)
return TypedValue.COMPLEX_UNIT_SP;
try{
String type=value.substring(value.length()-2, value.length());
if (type.equals("dp"))
return TypedValue.COMPLEX_UNIT_DIP;
else if (type.equals("sp"))
return TypedValue.COMPLEX_UNIT_SP;
else if (type.equals("pt"))
return TypedValue.COMPLEX_UNIT_PT;
else if (type.equals("mm"))
return TypedValue.COMPLEX_UNIT_MM;
else if (type.equals("in"))
return TypedValue.COMPLEX_UNIT_IN;
else if (type.equals("px"))
return TypedValue.COMPLEX_UNIT_PX;
}
catch(Exception e){
return -1;
}
return -1;
}
}
and this is the error that i got:
06-19 19:52:01.757 11580-11580/com.example.android.justify E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.justify/com.example.android.justify.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class ir.noghteh.JustifiedTextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.android.justify.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: ir.noghteh.JustifiedTextView
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.view.LayoutInflater.createView(LayoutInflater.java:552)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.android.justify.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
it's seems that the error says i need to inflate ir.noghteh.JustifiedTextView
but how should i have to do it
and main activity code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="JustifiedTextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<ir.noghteh.JustifiedTextView
xmlns:noghteh="http://noghteh.ir"
android:id="@+id/activity_main_jtv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
noghteh:text="@string/text_fa"
noghteh:textColor="@color/red"
noghteh:textSize="20sp" >
</ir.noghteh.JustifiedTextView>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="Default TextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<TextView
android:id="@+id/activity_main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
android:text="@string/text_fa"
android:textColor="@color/red"
android:textSize="20sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:orientation="horizontal"
android:padding="5dp" >
<SeekBar
android:id="@+id/activity_main_sb_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</SeekBar>
<LinearLayout
android:id="@+id/activity_main_ll_green"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/green"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_main_ll_red"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/red"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<TextView
android:id="@+id/activity_main_tv_fa"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="FA" />
<TextView
android:id="@+id/activity_main_tv_eng"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="EN" />
</LinearLayout>
</LinearLayout>
回答1:
change your xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="JustifiedTextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<com.example.android.justify.JustifiedTextView
xmlns:noghteh="http://noghteh.ir"
android:id="@+id/activity_main_jtv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
noghteh:text="@string/text_fa"
noghteh:textColor="@color/red"
noghteh:textSize="20sp" >
</com.example.android.justify.JustifiedTextView>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:text="Default TextView:"
android:textColor="@color/blue" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/gray_2" >
<TextView
android:id="@+id/activity_main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="25dp"
android:text="@string/text_fa"
android:textColor="@color/red"
android:textSize="20sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_1"
android:orientation="horizontal"
android:padding="5dp" >
<SeekBar
android:id="@+id/activity_main_sb_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</SeekBar>
<LinearLayout
android:id="@+id/activity_main_ll_green"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/green"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/activity_main_ll_red"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/red"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this is text"
/>
</LinearLayout>
<TextView
android:id="@+id/activity_main_tv_fa"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="FA" />
<TextView
android:id="@+id/activity_main_tv_eng"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:background="@color/gray_3"
android:gravity="center"
android:padding="5dp"
android:text="EN" />
</LinearLayout>
</LinearLayout>
来源:https://stackoverflow.com/questions/37911376/error-inflating-class-ir-noghteh-justifiedtextview