I've got this java file:
package com.example.tabbedapp;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
protected TextView ulamekLabel;
protected EditText licznik;
protected EditText mianownik;
protected TextView kUlamkowa;
protected TextView calosci;
protected TextView nLicznik;
protected TextView nMianownik;
protected TextView kreska;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
licznik.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
skracamy();
}
});
mianownik.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
skracamy();
}
});
return inflater.inflate(R.layout.fragment1_layout, container, false);
}
private void skracamy() {
if (TextUtils.isEmpty((CharSequence) licznik) || TextUtils.isEmpty((CharSequence) mianownik)) {
calosci.setText("0");
calosci.setVisibility(View.INVISIBLE);
nLicznik.setText("0");
nLicznik.setVisibility(View.INVISIBLE);
nMianownik.setText("0");
nMianownik.setVisibility(View.INVISIBLE);
kreska.setVisibility(View.INVISIBLE);
return;
}
int a = Integer.parseInt(licznik.getText().toString());
int b = Integer.parseInt(mianownik.getText().toString());
int l = (a / NWD(a, b));//.toString().toInt();
int m = (b / NWD(a, b));//.toString().toInt();
int c = (l / m);//.toString().toInt();
int d = (l % m);//.toString().toInt();
calosci.setVisibility(View.VISIBLE);
nLicznik.setVisibility(View.VISIBLE);
nMianownik.setVisibility(View.VISIBLE);
kreska.setVisibility(View.VISIBLE);
if (c != 0 && d != 0) {
calosci.setText(c);
nLicznik.setText(d);
nMianownik.setText(m);
} else if (d != 0) {
calosci.setText("");
nLicznik.setText(d);
nMianownik.setText(m);
} else {
calosci.setText(c);
nLicznik.setText("0");
nMianownik.setText("0");
}
}
int NWD(int a, int b)
{
if(b!=0)
return NWD(b,a%b);
return a;
}
}
And ofter debug I get this error: Attempt to invoke virtual method 'void android.widget.TextView.addTextChangedListener(android.text.TextWatcher)' on a null object reference
. I'm working on a fragment so I can't use findViewById(). Can anyone help? I just need to assign the view to a pointer eg. kreska = findViewById(R.id.kreska) but for fragments. I'm new to java. This file I'm actually translating from Kotlin to java but from main_activity to fragment.
CodePudding user response:
You declared the views but forgot to initialize them.
all views are uninitialized, that's why showing null object reference
Declaration
protected EditText licznik;
protected EditText mianownik;
Initliazation
licznik = findViewById(R.id.ID1);
mianownik = findViewById(R.id.ID2);
inflate and return view inside onCreateView
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.YOUR_FRAGMENT, container, false);
return view;
}
initiate view inside onViewCreated
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
licznik = view.findViewById(R.id.ID1);
mianownik = view.findViewById(R.id.ID2);
...
}
You can face some crashes when initializing the views inside onCreateView
because sometimes the view is not properly initialized. So always initialize views inside onViewCreated
.
Called immediately after onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) has returned, but before any saved state has been restored into the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.
onViewCreated
is a make sure that the view is fully created.