-3

I'm trying to create a floatingactionbutton with a listener to open a new view.

public class ConnectionMenu extends AppCompatActivity {
ConnectionManager conn = new ConnectionManager();
CredentialManager cred = new CredentialManager();
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_menu);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final EditText field1 = findViewById(R.id.editText);
    final EditText field2 = findViewById(R.id.editText2);
    final EditText field3 = findViewById(R.id.editText3);

    FloatingActionButton fab = findViewById(R.id.fab);
    Button button = findViewById(R.id.button2);
    final Spinner spinner = findViewById(R.id.spinner);
    final Intent intent = new Intent(this, CredentialMenu.class);
    Object[] credentials = cred.getAllnames();

    ArrayAdapter<Object> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_spinner_item, R.id.spinner, credentials);
    spinner.setAdapter(adapter);
    fab.setOnClickListener(new View.OnClickListener() { //fails here!!!!
        @Override
        public void onClick(View view) {
            startActivity(intent);
        }

But the exception in the title is throwed. The structure of my xml files is:

activity_connection_menu.xml:

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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="net.ddns.andrewnetwork.raspberrymonitor.View.ConnectionMenu">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_connection_menu" />

content_connection_menu.xml

...
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="match_parent"
            android:layout_height="84dp"
            android:layout_marginLeft="32dp"
            android:layout_marginStart="32dp"
            android:layout_weight="1"
            android:clickable="true"
            android:focusable="true"
            app:backgroundTint="@color/colorPrimaryDark"
            app:srcCompat="@android:drawable/ic_menu_add" />

    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="40dp"
        android:layout_weight="1"
        android:text="@string/button1"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="206dp" />

</LinearLayout>

Seems like that the fab variable is instantiated but actually null in its contents. Any ideas?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Andrew
  • 115
  • 8

1 Answers1

0

Either post your complete error. Or there is at least one TextView which you have not initialized but used somewhere in code (MOST LIKELY AT THE INDICATED LINE OF THE ERROR) find that text view and initialize it ie

tv = (TextView) findViewById(R.id.yourID);
Aqsa Shahid
  • 64
  • 1
  • 9