【问题标题】:Fragment is inflated but method has no affect片段膨胀但方法没有影响
【发布时间】:2014-06-10 17:30:47
【问题描述】:

我是 android/java 开发的新手,我在正确实现我的方法“updateBinaryInput()”时遇到问题。在我的代码中,我膨胀了一个片段,为了简单起见,我想说我想做的就是在当前膨胀的布局中设置一个文本视图。我不确定是否需要从 main_activity 调用此方法。据我了解,由于布局文件是通过片段“片段”膨胀的,因此处理膨胀的 layout.xml 的方法应该在片段中。有人可以给我一个很好的例子,说明处理当前片段布局的方法的良好实现。
以下是我的片段:

package com.example.tabswithswipe;

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

public class binaryToDecimalFragment extends Fragment {



    /////initialize text views////
    TextView binaryInputTextView;
    TextView binaryTitleTextView;
    TextView convertTextView;
    TextView result2TextView;

    /////initialize buttons////
    Button one2Button;
    Button zero2Button;
    Button clear2Button;
    Button bck2Button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        updateBinaryInput();

    }


    ///my method////
    public void updateBinaryInput(){
        //should change the text view of the binary input
        //binaryInputTextView.setText(binaryInputString);
        binaryInputTextView.setText("sometext");

     }


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

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


    ////link ui objects///
        binaryTitleTextView= (TextView) rootView.findViewById(R.id.binaryTitleTextView);
        convertTextView= (TextView)rootView.findViewById(R.id.converterTitleTextView);
        binaryInputTextView=(TextView)rootView.findViewById(R.id.binaryInputTextView);
        result2TextView=(TextView)rootView.findViewById(R.id.result2TextView);

        ////initialize buttons////
         one2Button=(Button)rootView.findViewById(R.id.one2Button);
         zero2Button=(Button)rootView.findViewById(R.id.zero2Button);
         clear2Button=(Button)rootView.findViewById(R.id.clear2Button);
         bck2Button=(Button)rootView.findViewById(R.id.bck2Button);

        return rootView;
    }
}

【问题讨论】:

    标签: android android-fragments methods android-inflate


    【解决方案1】:

    您必须了解lifecycle of a FragmentonCreate()onCreateView() 之前被调用。如果您在 onCreate() 中调用 updateBinaryInput() 方法,则您要更新的 TextView 不会被初始化。我认为您会因此收到 NullPointerException。

    在你初始化 TextView 之后在 onCreateView 中调用你的方法。

    类似这样的:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    
    ///my method////
    public void updateBinaryInput(){
        //should change the text view of the binary input
        //binaryInputTextView.setText(binaryInputString);
        binaryInputTextView.setText("sometext");
    
     }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {        
    
        View rootView = inflater.inflate(R.layout.binarytodecimal, container, false);
    
    
    ////link ui objects///
        binaryTitleTextView= (TextView) rootView.findViewById(R.id.binaryTitleTextView);
        convertTextView= (TextView)rootView.findViewById(R.id.converterTitleTextView);
        binaryInputTextView=(TextView)rootView.findViewById(R.id.binaryInputTextView);
        result2TextView=(TextView)rootView.findViewById(R.id.result2TextView);
    
        ////initialize buttons////
         one2Button=(Button)rootView.findViewById(R.id.one2Button);
         zero2Button=(Button)rootView.findViewById(R.id.zero2Button);
         clear2Button=(Button)rootView.findViewById(R.id.clear2Button);
         bck2Button=(Button)rootView.findViewById(R.id.bck2Button);
    
        updateBinaryInput(); // invoke methode after init the Views
        return rootView;
    }
    

    【讨论】:

    • 谢谢,我应该知道的。
    • NP,至少你现在知道了。
    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多