Button Click Causes App To Crash

The previous solutions (by @GueorguiObregon and @MuhammadFaisalHyder) work but were not the thing that I was wishing. I found out the problem came from setting the android:theme attribute to the view (in my case), and also is related to the AppCompat library (see this).

So I simply removed the android: namespace from this line (from the view's style):

<item name="android:theme">@style/someTheme</item>

and made it likes:

<item name="theme">@style/someTheme</item>

and it works fine.

The amazing thing is the problem is only on the high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both ways (with or without android: namespace) work.

Also, see @MateiSuica comment below if you want to insert theme directly to the element (without using a style).


why not doing it the typical way? Try normally like by Declaring

public class AdminControl extends AppCompatActivity {
    //....
    FloatingActionButton mFAB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_control);

        Toolbar b = (Toolbar) findViewById(R.id.toolbar);
        //....
        mFAB = (FloatingActionButton) findViewById(R.id.fab);
        mFAB.setOnClickListener(new View.OnClickListener(){      

            @Override
            public void onClick(View view) {
                 Intent newIntent = new Intent(this, TournamentCreator.class);
                 startActivity(newIntent);
            }                
        });
    }

And use XML for floating button like this:

<android.support.design.widget.FloatingActionButton
 android:id="@+id/fab"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom|end"
 android:layout_margin="@dimen/fab_margin"
 android:src="@android:drawable/ic_input_add"     
/>

This is the common way and clean way to do it, hope i helped.


Your problem should be solved removing your android:onClick="createNewTournament" event from your layout

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add"   
    android:clickable="true" />

And adding a listener to R.id.fab in your onCreate, like this.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin_control);
    Toolbar b = (Toolbar) findViewById(R.id.toolbar);
    b.setTitle("Tournaments");
    setSupportActionBar(b);
    ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
    if (tournaments == null){
        tournaments = new ArrayList<>();
    }

    FloatingActionButton myFab = (FloatingActionButton)findViewById(R.id.fab); 
    myFab.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
            createNewTournament(v); 
        } 
    });
}

The same problem happen in this question EditText OnClick Exception and was fixed using listener.

Hope this helps!!

Tags:

Java

Xml

Android