Instance element of an XML in Android

1

I have a method where I'm trying to instantiate a TextView to get its string string and be able to perform an update and display it on the screen. My problem is that I can not reference it, I have the code like that.

private void actualizarInfoSaldo() {
    Saldo saldo;
    TextView textoSaldo = (TextView) findViewById(R.id.saldoTextView); //FALLA !!!!

    OperacionesBaseDatos bdOperaciones= new OperacionesBaseDatos();
    saldo = bdOperaciones.leerSaldo();

    String saldoStr = "Saldo actual: " + String.valueOf(saldo.getSaldo()) + " $";

    CharSequence charSqc;
    charSqc = saldoStr;
    textoSaldo.setText(charSqc);
}

ERROR:

FATAL EXCEPTION: main
                                               Process: com.example.iberd.actionvalue, PID: 12210
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                   at com.example.iberd.actionvalue.ScreenSlidePageFragment.actualizarInfoSaldo(ScreenSlidePageFragment.java:240)
                                                   at com.example.iberd.actionvalue.ScreenSlidePageFragment.cambioSaldo(ScreenSlidePageFragment.java:218)
                                                   at com.example.iberd.actionvalue.ScreenSlidePageFragment$1.onClick(ScreenSlidePageFragment.java:132)
                                                   at android.view.View.performClick(View.java:5637)
                                                   at android.view.View$PerformClick.run(View.java:22429)
                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

XML:

<LinearLayout
    android:id="@+id/saldoLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/saldoTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/saldo"
        android:textAlignment="center"
        android:textColor="@color/letra"
        android:textSize="24sp" />

</LinearLayout>

The issue is that I have several xml that are referenced from several activitys, but in all cases the textView id is balanceTextView

    
asked by Eduardo 28.05.2017 в 03:54
source

1 answer

1

If you are calling it within fragent , you need to use the View , in onCreateView assign the TextView in this way:

TextView textoSaldo;

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

   View rootView = inflater.inflate(R.layout.tu_fragment, container, false);
   textoSaldo = rootView.findViewById(R.id.saldoTextView);

}
    
answered by 28.05.2017 / 14:46
source