Frage

I am new to android/java development and i am having issues with implementing my method "updateBinaryInput()" correctly. In my code I inflate a fragment and for the sake of simplicity lets say all i wanted to do is set a text view in the layout that was currently inflated. I'm unsure if i need to call this method from the main_activity or not. From my understanding since the layout file is inflated through the fragment "fragment" the methods that handle the layout.xml that is inflated should be in the fragment. Can someone please give me a good example of a good implementation of a method which handles the current fragment's layout.
Below is my fragment:

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;
    }
}
War es hilfreich?

Lösung

You have to understand the lifecycle of a Fragment. The onCreate() is called before onCreateView(). If you invoke your updateBinaryInput() method in onCreate() the TextView you wanna update isn't initialized. I think you get a NullPointerException because of this.

Call your method in onCreateView after you initizalized the TextView.

Something like this:

@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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top