Using button.getTag() causes the app to Crash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using button.getTag() causes the app to Crash



whenever I try to get .tag() My App Crashes. It's been a while and , i can't solve the problem....


public class MainActivity extends AppCompatActivity

public void translateFrench(View view)

Button button = (Button) view;

Log.i("Nothing", button.getTag().toString());


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




XML:


<android.support.v7.widget.GridLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:columnCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="4">

<Button
android:id="@+id/button1"
android:text="Do you speak nEnglish?"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1"
android:onClick="translateFrench"/>

<Button
android:id="@+id/button2"
android:text="Good Evening"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_rowWeight="1"
android:onClick="translateFrench"/>

</android.support.v7.widget.GridLayout>



Error Log:



Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at





where u call translateFrench() ???
– Kanaan El Bhissy
Aug 7 at 17:10





It is onClick method.. For buttons.
– Ashraful Alam Imran
Aug 7 at 17:11





Please edit your question and post error log
– Shashanth
Aug 7 at 17:12





share the code of on click, point where you are calling translateFrench. I have a feeling you are sending the wrong view
– Abubakar
Aug 7 at 17:12






You should use setTag method before calling getTag. Please make sure that you care doing that.
– Sudhi
Aug 7 at 17:16




1 Answer
1



you need to call setTag() before getTag()
I think you can find useful information at this post



What is the main purpose of setTag() getTag() methods of View?





Do I have to add Tags in xml for each of the buttons..? Or I can do it inside the java part. I'm extremely new in this.
– Ashraful Alam Imran
Aug 7 at 17:34





Do it inside java part
– Kanaan El Bhissy
Aug 8 at 20:51






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard