R
R
Riky_Maritn2019-10-23 00:42:43
Android
Riky_Maritn, 2019-10-23 00:42:43

Android Fragments. Why get NullPointerException in some cases?

The bottom line is, I have two versions of the code, which in my opinion should do the same thing. But in one of the versions NullPointerException is obtained.
What am I trying to do?
I need to get the data entered into the EditText, the EditText is in a fragment.
The code below works.

Working code

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fm;
private FragmentTransaction ft;
private MyFragment mf;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView_text_of_fragment);
fm = getSupportFragmentManager();
mf = new MyFragment();
}
@Override
public void onClick(View v) {
ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.add_button:
if (fm.findFragmentByTag(MyFragment.FRAGMENT_TAG) == null) {
Toast.makeText(this, "Is add button!", Toast.LENGTH_SHORT).show();
ft.add(R.id.fragment_layout, mf, MyFragment.FRAGMENT_TAG);
Toast.makeText(this, "Fragment is added!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.set_text_button:
if(fm.findFragmentByTag(MyFragment.FRAGMENT_TAG)!= null){
EditText text = findViewById(R.id.editText);
textView.setText(textView.getText() + text.getText().toString());
Toast.makeText(this, "Text is added!", Toast.LENGTH_SHORT).show();
}
break;
/* case R.id.remove_button:
if (fm.findFragmentByTag(MyFragment.FRAGMENT_TAG) != null) {
EditText text = findViewById(R.id.editText);
text.setText("");
Toast.makeText(this, "Fragment is removed!", Toast.LENGTH_SHORT).show();
ft.remove(mf);
}
break;*/
}
ft.commit();
}
}

Next, the second version of the code, which throws a NullPointerException, on the setText() method.
I tried to check if the fragment exists in the manager, and I got flase.
Not working code

package com.example.mkiel.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fm;
private FragmentTransaction ft;
private MyFragment mf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();
mf = new MyFragment();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_button:
ft = fm.beginTransaction();
if (fm.findFragmentByTag(MyFragment.FRAGMENT_TAG) == null) {
ft.add(R.id.fragment_layout, mf, MyFragment.FRAGMENT_TAG);
ft.commit();
ft = fm.beginTransaction();
EditText text = findViewById(R.id.editText);
text.setText("При вставке получается null исключение");
ft.commit();
}
break;
/* case R.id.set_text_button:
if (fm.findFragmentByTag(MyFragment.FRAGMENT_TAG) != null) {
EditText text = findViewById(R.id.editText);
textView.setText(textView.getText() + text.getText().toString());
}
break;*/
/* case R.id.remove_button:
if (fm.findFragmentByTag(MyFragment.FRAGMENT_TAG) != null) {
EditText text = findViewById(R.id.editText);
text.setText("");
Toast.makeText(this, "Fragment is removed!", Toast.LENGTH_SHORT).show();
ft.remove(mf);
}
break;*/
}
}
}

Fragment Code

public class MyFragment extends Fragment {
public final static String FRAGMENT_TAG = "FragmentOne";
public View view;
@Override
public View getView() {
return super.getView();
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_layout, container, false);
return view;
}
}

Help me understand what I'm doing wrong and why I get NullPointerException in the second version of the code.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-10-23
@Riky_Maritn

You're not working with fragments correctly.
As I understand it, this textview, where you are trying to set the text, is in a fragment. Don't do that, it breaks encapsulation. Only the fragment itself should change its views.
Further, the commit occurs asynchronously. And the fact that you are trying, as it were, to do something between the start of the transaction and the commit, obviously hoping that it will fall into the transaction, is wrong. Transactions are for fragments only.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question