Skip to content Skip to sidebar Skip to footer

Stuck On Why My Android App Won't Work?

I am working on an app which will do the following: Allow the first user 'Sam' to type his/her message in the editText field. Press the 'Alex' button at the bottom of the screen t

Solution 1:

android:onClick="startalexbutton"

This tells your program to call a method startalexbutton() whenever it is pressed, and this method must take a View parameter (public void startalexbutton(View view)).

This method would go in your MainActivity class; however, since you already use setOnClickListener() for it, just removeandroid:onClick="startalexbutton" from your XML. (You can remove android:onClick="startMainActivity" as well.)

PS: For the future, please put your LogCat logs in text format; then they A) show up in search results and B) are easier to read.

Solution 2:

It's because you registered the button clicks with the following lines for each button in xml.

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1"
        android:onClick="startalexbutton" />

 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button2"
        android:onClick="startMainActivity" />

But you haven't defined any onClick() methods with this names. Either remove these onclick lines from xml or define these methods with the same names for listening to the buttons instead of setting again onClickListeners(). If you decide to define onClick methods with the names that you registered in xml, do the following.

publicvoidstartalexbutton(View view)
{
// Do what ever you need here when button is clicked. Because the control comes to this method when the button is clicked. 
}

If you do this, you don't need the initialize() method anymore.

Solution 3:

you are not calling initialize() in onCreate of ActivityAlex and MainActivity Activity to initialize Button

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

          initialize() ;//<<<<initialize here
    }

and remove onClick Attribute from both button in xml as

In activity_main.xml :

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1"
       />

In activityalex.xml :

 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button2"
         />

NOTE :

when you are adding setOnClickListener for any View in java code then not need to add android:onClick with View xml

Post a Comment for "Stuck On Why My Android App Won't Work?"