Greetings! I am currently developing an application and facing an issue that requires assistance.
log cat of the error:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at cqdevelopers.incrediblediet.Fragment3.getAddressKeyValueFile(Fragment3.java:90)
at cqdevelopers.incrediblediet.Fragment3.onCreateView(Fragment3.java:25)
<!-- begin snippet: js hide: false -->
<!-- language: lang-js -->
05-16 02:25:21.711 6498-6498/cqdevelopers.incrediblediet E/AndroidRuntime: FATAL EXCEPTION: main
Process: cqdevelopers.incrediblediet, PID: 6498
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at cqdevelopers.incrediblediet.Fragment3.getAddressKeyValueFile(Fragment3.java:90)
at cqdevelopers.incrediblediet.Fragment3.onCreateView(Fragment3.java:25)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:254)
at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1920)
at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2050)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.Grou...
Fragment3.java:
public class Fragment3 extends Fragment {
private EditText editText;
private TextView textView;
private Button button;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment3_layout, container, false);
getAddressKeyValueFile();
editText = (EditText) v.findViewById(R.id.tvadress);
textView = (TextView) v.findViewById(R.id.textView);
button = (Button) v.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String address = null;
address = editText.getText().toString();
textView.setText(address);
setAddressKeyValueFile(address);
}
});
return v;
}
private void getAddressKeyValue() {
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("r", Context.MODE_PRIVATE);
String key = getString(R.string.address);
String existingAddress = sharedPreferences.getString(key, null);
if (existingAddress != null) {
TextView textView = (TextView) getActivity().findViewById(R.id.textView);
textView.setText(existingAddress);
}
}
private void setAddressKeyValue(String address)
{
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("d" ,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String key = getString(R.string.address);
editor.putString(key,address);
editor.commit();
}
private void getAddressKeyValueFile()
{
Context context = getContext().getApplicationContext();
String fileName = getString(R.string.filename);
SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
String key = getString(R.string.address);
String existingAddress = sharedPreferences.getString(key,null);
if(existingAddress != null)
{
TextView textView = (TextView) getActivity().findViewById(R.id.textView);
textView.setText(existingAddress);
}
}
private void setAddressKeyValueFile(String address)
{
Context context = getContext().getApplicationContext();
String fileName = getString(R.string.filename);
SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String key = getString(R.string.address);
editor.putString(key, address);
editor.commit();
}
}
And fragment3.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cqdevelopers.incrediblediet.MainActivity$PlaceholderFragment">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvadress"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="register"
android:id="@+id/textView2"
android:layout_marginTop="23dp"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/tvadress"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView"
android:layout_marginTop="29dp"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
If you could take a look at the exception and guide me where I might have made an error, your valuable assistance would be greatly appreciated. Thank you in advance.