Class Button Inflating Error

I just started to study computer science and Android development. I tried some helloworld demos to try and learn.

So, continuing wildly, I tried to rewrite a program that has one button for two buttons using onClickListener. Although I don't have compilation errors, my program closes me:

01-05 11: 20: 33.968: E / AndroidRuntime (3257): java.lang.RuntimeException: Cannot start Activity ComponentInfo {com.example.multbuttontest / com.example.multbuttontest.MultiButtonActivity}: android.view. InflateException: binary string of XML file # 16: class button bloat error

My XML file looks like this (sorry to suck on formatting):

<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MultiButtonActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

and code:

 package com.example.multbuttontest; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; public class MultiButtonActivity extends Activity implements View.OnClickListener{ Button button1, button2; int touchCount1, touchCount2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_multi_button); button1 = (Button) findViewById(R.id.button1); button1.setText( "Touch Button 1!"); button1.setOnClickListener(this); button2 = (Button) findViewById(R.id.button2); button2.setText( "Touch Button 2!"); button2.setOnClickListener(this); } public void onClick(View v){ switch(v.getId()){ case R.id.button1: touchCount1++; button1.setText("Touched b1 " + touchCount1 + " times(s)"); break; case R.id.button2: touchCount2++; button2.setText("Touched b2 " + touchCount2 + " times(s)"); break; } } } 

This is strictly for my training purposes, and the code really sucks. Any help would be appreciated.

+4
source share
2 answers

Here is some code that can help you.

MainActivity:

 public class MainActivity extends Activity { private TextView txt1; private Button btnCount,btnClear; private int num1 = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt1 = (TextView) findViewById(R.id.txt1); btnCount = (Button) findViewById(R.id.button1); btnClear = (Button) findViewById(R.id.button2); } public void sendMessage(View view) { txt1.setText("You have clicked"+ (num1) + "times"); num1++; } public void clear(View view) { num1 = 1; txt1.setText("You have clicked"+ (num1) + "times"); } } 

activity_main.xml:

  <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_clear" android:onClick="clear" /> <TextView android:id="@+id/txt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+string/pushed" /> 

Output:

enter image description here

enter image description here

+1
source

This is not a button , it button . The XML tag points to the java class within the framework. And Java is case sensitive.

 <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
+8
source

Source: https://habr.com/ru/post/1480820/


All Articles