【问题标题】:LayoutInflater wordSpanLayoutInflater wordSpan
【发布时间】:2014-02-24 18:05:02
【问题描述】:

我想在 OnCreateView 设置 wordtoSpan 字符串。

我的代码:

package com.tachles;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class History_moadim_a extends Fragment {

    TextView horef;
    TextView kaiz;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);

        return rootView;

        horef = (TextView) findViewById(R.id.textView3);
        Spannable wordtoSpan = new SpannableString("hgh");
        horef.setText(wordtoSpan);



    }
}

Eclipse 说我不能使用 findViewById,所以我尝试插入 getView() 但仍然是同样的问题。请告诉我我的错误

谢谢

【问题讨论】:

  • 请看答案,希望对你有帮助!!
  • 答案更新了,请看

标签: java android html


【解决方案1】:

如果history_moadim_a 是包含textView3 的布局,请将onCreateView() 更改为类似

View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);

horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);

return rootView;

您想在刚刚膨胀的布局中找到TextView (rootView),并在结束处返回布局(而不是在方法的中间)。

【讨论】:

    【解决方案2】:

    使用

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.text.Spannable;
    import android.text.SpannableString;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class History_moadim_a extends Fragment {
    
        TextView horef;
        TextView kaiz;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
    
    
    
            horef = (TextView) rootView.findViewById(R.id.textView3);
            Spannable wordtoSpan = new SpannableString("hgh");
            horef.setText(wordtoSpan);
    
     return rootView;
    
        }
    }
    

    我在完美的地方看不到 onCreateView 的返回语句,所以请在 onCreateView 的末尾通过 onCreateView 的方式在 onCreateView 中执行return rootView ;

    【讨论】:

    • 你没有完美地返回视图,请像 return rootView 一样;在 oncreateView 结束时
    • 请将视图返回为 return rootView ;
    • getView() inside onCreateView() 将不起作用 - 您尚未将视图返回到它可以通过 getView() 返回的框架。
    • 现在Fragment 中没有findViewById()
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多