片段被夸大但方法没有影响

时间:2014-04-25 06:29:19

标签: android android-fragments methods android-inflate

我是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;
    }
}

1 个答案:

答案 0 :(得分:1)

您必须了解lifecycle of a Fragment。在onCreate()之前调用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;
}