Making a code to simply show Hello, world in a TextView named st, which is referred to by the variable stat, the error comes out:
java.lang.RuntimeException: Unable to start activity ComponentInfo {com.aapps.vyber.clicker/com.aapps.vyber.app.MainActivity}: java.lang.NullPointerException:
Referring to the last line of instructions onCreate (), in which I try to place the text by means of setText ().
MainActivity.xml - > You only have the options bar below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"
android:layout_above="@+id/navigation"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java - > With this class, I want to change the text of a TextView of Fragment.xml
package com.aapps.vyber.app;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView stat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
stat = (TextView) findViewById(R.id.st);
stat.setText("Hola, mundo");
}
}
I also defined a new method by which type text is put,
void do_it(){
stat.setText("Hola, mundo");
}
To invoke it from onCreate () instead of putting the setText () directly, but it still does not work.
Here is the XML of the Fragment. The MainActivity is completely empty, so I do not see its inclusion important
Fragment.xml - > It has the TextView that I want to reference
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/st"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="403dp"
android:text="Quiero cambiarme"
android:textAlignment="center"
android:textSize="18sp" />
</RelativeLayout>
FragmentActivity.java - > You only have the code to change according to the toolbar
package com.aapps.vyber.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentActivity extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.Fragment, container, false);
}
}
Any big mistake out there? Thank you for your dedication and help in advance.
NOTE
st exists and is in a layout, only not in the MainActivity. It is in a fragment. If that is the reason for the problem, of course, I'd love to be notified of how to fix it by keeping the TextView in the snippet. However, the IDE does not point me to the text as impossible to achieve. Also, all the variables and values that I use are defined and I have tried not to use import methods that can return null values