0

Could someone help me? the moment I press on the FloatingActionButton the activity is closed and goes back. What am I doing wrong?

    public class Regolamento extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regolamento);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "English", Snackbar.LENGTH_LONG);
                TextView tv = (TextView) findViewById(R.id.text);
                tv.setText("ciao");

            }
        });
    }}
<androidx.core.widget.NestedScrollView
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".Regolamento"
        tools:showIn="@layout/activity_regolamento">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:text="wow"/>
    </androidx.core.widget.NestedScrollView>

How should i do? Can someone help me? until just before everything worked then suddenly it stopped working

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.bbmarinablu.Regolamento$1.onClick(Regolamento.java:29) at android.view.View.performClick(Unknown Source:17) at android.view.View.performClickInternal(Unknown Source:3) at android.view.View.access$3500(Unknown Source:0) at android.view.View$PerformClick.run(Unknown Source:8) at android.os.Handler.handleCallback(Unknown Source:2) at android.os.Handler.dispatchMessage(Unknown Source:4) at android.os.Looper.loop(Unknown Source:242) at android.app.ActivityThread.main(Unknown Source:98) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11) at com.android.internal.os.ZygoteInit.main(Unknown Source:275)

8
  • Post the full xml code and imported classes Feb 15, 2020 at 13:34
  • 1
    there is nothing in onClick that could lead to an error, the problem is somewhere else
    – i30mb1
    Feb 15, 2020 at 13:39
  • 1
    By going back do you mean it crashes, in which case you should be able to view the error in Logcat to see what happens. From what you have posted here since I dont see android:id="@+id/text" in your xml I'm assuming you get a null pointer exception causing a crash. Posting some helpful code would help us locate the issue if this isn't the cause
    – ljk
    Feb 15, 2020 at 16:41
  • Please post the error message you are getting. You can find it in the Run tab in Android Studio. Feb 15, 2020 at 18:22
  • I added the error I get from the app above when I start it
    – Giovanni
    Feb 15, 2020 at 23:58

1 Answer 1

1

You need to define id text in activity_regolamento.xml.

Add android:id="@+id/text" in TextView element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.