文本样式在自定义文本中不起作用

时间:2016-12-17 11:49:58

标签: android android-layout android-recyclerview textview

这是我的自定义文字类:

title = (TextView)view.findViewById(R.id.post_title);
            title.setTypeface(title.getTypeface(), Typeface.BOLD);

但在xml布局中使用android:textStyle =“bold”表示自定义文本,文字样式不起作用! 我尝试以编程方式使用set text样式:

java.lang.NullPointerException at com.example.erfan.memaraneha.maghalat.DataAdapter$ViewHolder.<init>(DataAdapter.java:94)

工作正常,但当Recyclerview应用程序崩溃结束并发出此错误时:

 title.setTypeface(title.getTypeface(), Typeface.BOLD);

从这一行:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    List<jsonContent> jcontent;



    public DataAdapter(Context context,List<jsonContent> jcontent) {

        this.context=context;
        this.jcontent=jcontent;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

       View view;
        if(i == R.layout.card_row) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        }else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder,int i) {


        if(i == jcontent.size()) {
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Button Clicked", Toast.LENGTH_LONG).show();
                }
            });
        }
        else {

            viewHolder.title.setText(jcontent.get(i).title);

            Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView);
        }
    }

    @Override
    public int getItemCount() {

            return jcontent.size()+1;

    }


    @Override
    public int getItemViewType(int position) {
        return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView title;

        private ImageView imageView;

        private Button button;

        public ViewHolder(final View view) {
            super(view);


           title = (TextView)view.findViewById(R.id.post_title);
            title.setTypeface(title.getTypeface(), Typeface.BOLD);

            imageView=(ImageView)view.findViewById(R.id.img);
            button=(Button)view.findViewById(R.id.loadmore);



            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(view.getContext(),card_activity.class);
                    Bundle b = new Bundle();
                    String passingdata = title.getText().toString();
                    b.putString("Key", passingdata);
                    intent.putExtras(b);
                    view.getContext().startActivity(intent);
                }
            });
        }
    }
}

这是我的适配器:

    2016-12-17 12:32:09 [pool-10-thread-4] ERROR com.agilemind.commmons.io.searchengine.analyzers.aC - 
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.reflect.GeneratedConstructorAccessor32.newInstance(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_111]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source) ~[?:1.8.0_111]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) ~[?:1.8.0_111]
    at java.net.HttpURLConnection.getResponseCode(Unknown Source) ~[?:1.8.0_111]
    at java.net.HttpURLConnection.getResponseMessage(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseMessage(Unknown Source) ~[?:1.8.0_111]
    at com.agilemind.commons.io.pagereader.url.URLPageReader.getContent(URLPageReader.java:78) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.cache.b.retrieve(b.java:2) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.cache.b.retrieve(b.java:1) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.cache.impl.LockedCache.get(LockedCache.java:10) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.cache.CachedPageReader.getContent(CachedPageReader.java:9) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.AjaxPageReader.getContent(AjaxPageReader.java:4) ~[commons.jar:?]
    at com.agilemind.commons.io.pagereader.PageReaderUtil.getResponseCodeFollowSingleRedirect(PageReaderUtil.java:9) ~[commons.jar:?]
    at com.agilemind.commmons.io.searchengine.analyzers.aC.a(aC.java:4) ~[searchengines.jar:?]
    at com.agilemind.commmons.io.searchengine.analyzers.j.a(j.java:10) ~[searchengines.jar:?]
    at com.agilemind.commmons.io.searchengine.analyzers.j.check(j.java:15) ~[searchengines.jar:?]
    at com.agilemind.commmons.io.searchengine.analyzers.f.check(f.java:3) ~[searchengines.jar:?]
    at com.agilemind.commons.application.modules.io.cache.a.retrieve(a.java:3) ~[commons-application.jar:?]
    at com.agilemind.commons.application.modules.io.cache.a.retrieve(a.java:1) ~[commons-application.jar:?]
    at com.agilemind.commons.io.pagereader.cache.impl.LockedCache.get(LockedCache.java:10) ~[commons.jar:?]
    at com.agilemind.commons.application.modules.io.cache.FactorValueCache.get(FactorValueCache.java:18) ~[commons-application.jar:?]
    at com.agilemind.sitescan.util.operations.UpdateDomainFactorOperation.execute(UpdateDomainFactorOperation.java:11) ~[websiteauditor.jar:?]
    at com.agilemind.commons.modules.concurrent.util.operations.Operation.executeOperation(Operation.java:126) ~[commons.jar:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_111]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.Alerts.getSSLException(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) ~[?:1.8.0_111]
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source) ~[?:1.8.0_111]
    at java.net.URLConnection.getContentEncoding(Unknown Source) ~[?:1.8.0_111]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContentEncoding(Unknown Source) ~[?:1.8.0_111]
    at com.agilemind.commons.io.pagereader.url.URLPageReader.getContent(URLPageReader.java:97) ~[commons.jar:?]
    ... 21 more

4 个答案:

答案 0 :(得分:0)

使用它,这是有效的:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
            setTypeface(tf);
        }
    }
}

答案 1 :(得分:0)

在此行下方设置输入面。从 ViewHolder 中删除它。你的字体  文件应位于 asset / fonts 文件夹中。

viewHolder.title.setText(jcontent.get(i).title);

Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/gandom-bold.ttf"); 

viewHolder.title.setTypeface(face);

答案 2 :(得分:0)

经过一番尝试后我使用这一行

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    INIT_FOLD
    TreatMsg Application.GetNamespace("MAPI").GetItemFromID(EntryIDCollection)
End Sub

下的

viewHolder.title.setTypeface(viewHolder.title.getTypeface(), Typeface.BOLD);

在我的onBindViewHolder和问题解决

答案 3 :(得分:0)

设置TypeFace时,在自定义Textview中传递文本样式。我更改了自定义Textview类。这将有希望地发挥作用。

public class CustomTXT  extends TextView {


public CustomTXT(Context context) {
    super(context);
    Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
    this.setTypeface(face, Typeface.BOLD);
}

public CustomTXT(Context context, AttributeSet attrs) {
    super(context, attrs);
    Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
    this.setTypeface(face, Typeface.BOLD);
}

public CustomTXT(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    Typeface face=Typeface.createFromAsset(context.getAssets(), "gandom-bold.ttf");
    this.setTypeface(face, Typeface.BOLD);
}

protected void onDraw (Canvas canvas) {
    super.onDraw(canvas);


}

我添加了&#34; Typeface.BOLD&#34;在setTypeFace方法中,因此您不再需要在xml或Java代码中显式设置textStyle。