Solution 1 :
Please change the button’s below statement which needs to indicate correct method when clicking actions made.
Please change in XML file of button from,
android:onClick="click"
to,
android:onClick="onClick"
Otherwise, please change method name in Java file as "click()"
Problem :
I am new in android learning Using this My app name is Threads.In Xml there is one Textview and button. When I click on button there will app shows “Threads has stopped” .
In this app I’m learning about how to us threads while developing app.
package com.codewithjarvis.threads;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view)
{
Button Vbutton =(Button)findViewById(R.id.Vbutton);
long Ltime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis()< Ltime)
{
synchronized (this)
{
try{
wait(Ltime - System.currentTimeMillis());
}catch (Exception e){}
}
}
TextView Vtext = (TextView)findViewById(R.id.Vtext);
Vtext.setText("hello frands this is surbhi");
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android_id="@+id/Vtext"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginTop="120dp"
android_text="TextView"
android_textSize="30sp"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent" />
<Button
android_id="@+id/Vbutton"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginTop="296dp"
android_onClick="click"
android_text="Button"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.498"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toBottomOf="@+id/Vtext" />
</androidx.constraintlayout.widget.ConstraintLayout>
why my app showing stopped in android phone? is there any bug or any error in my code
when I’m running this code it shows
Send bug report to Mi for analysis? This report may contain personally identifiable information. your report will be used to help fix this bug and will never be shared in any commercial context.
Comments
Comment posted by Surbhi Bansal
Same problem comes in my other program too…So how can I check where is the problem in my program?
Comment posted by Gowrishankar
@SurbhiBansal when you gave method which needs to trigger while doing click action, the method name gave in XML file and defined method name in JAVA file should be the same. If its mismatch, the crash will occur and the app stops unexpectedly.