Answer the question
In order to leave comments, you need to log in
Why does Fragment not work correctly when the device is rotated?
When displaying a fragment on the screen after rotating the device, I get the following error:
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.user.miniapppikflowtwo.FragmentTipTextView.setView(FragmentTipTextView.java:31)
at com.example.user.miniapppikflowtwo.MainActivity$2.onClick(MainActivity.java:158)
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTipTextView extends Fragment {
TextView textView;
String string;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tip_tv, container,false);
textView = view.findViewById(R.id.fragment_tip_text_view);
return view;
}
public void setTextView(String string){
this.string = string;
}
public void setView(){
textView.setText(string); //ОШИБКА ТУТ
}
}
package com.example.user.miniapppikflowtwo;
import android.animation.PropertyValuesHolder;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.PersistableBundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.db.chart.animation.Animation;
import com.db.chart.listener.OnEntryClickListener;
import com.db.chart.model.LineSet;
import com.db.chart.tooltip.Tooltip;
import com.db.chart.util.Tools;
import com.db.chart.view.LineChartView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements Dialog2.OnFragmentListener {
LineChartView chart;
DBRecorder dataBase;
Toolbar toolbar;
DialogFragment dlg1, dlg2;
FragmentManager fragmentManager;
FragmentTipTextView fragmentTipTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
fragmentTipTextView = new FragmentTipTextView();
if(fragmentManager.findFragmentByTag("TodayFragment") == null){
System.err.println("Fragment был null");
fragmentManager.beginTransaction().add(R.id.tip_container, fragmentTipTextView, "TodayFragment").commit();
} else {
System.err.println("Fragment не был null");
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("TodayFragment")).commit();
}
chart = findViewById(R.id.chart);
dataBase = new DBRecorder(getApplicationContext());
dlg1 = new Dialog1();
dlg2 = new Dialog2();
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dlg2.show(getFragmentManager(), "dlg2");
}
});
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); //поддержка Android < 5.0
getSupportActionBar().setTitle("Моя пыльца");
setGraph();
}
String[] stringDate; //строки с датами
float[] values; // значения
private void setGraph(){
//Тут настраивается внешний вид графика. К проблеме отношения не имеет
chart.setOnEntryClickListener(new OnEntryClickListener() {
@Override
public void onClick(int setIndex, int entryIndex, Rect rect) {
Recorder recorder = arrayList.get(entryIndex);
fragmentTipTextView.setTextView(recorder.getTip());
fragmentTipTextView.setView(); //ОШИБКА В ЭТОЙ СТРОКЕ
}
});
//-------Показываем график-------
chart.show(anim); //Показывает график с анимацией
}
//Другие методы, не влияющие на fragment
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Toolbar"/>
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar">
<com.db.chart.view.LineChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_marginVertical="8dp"
android:layout_height="250dp">
</com.db.chart.view.LineChartView>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/tip_container"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView">
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_24dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_tip_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textColor="@color/primaryTextColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question