Using Build->Build APK, I tried to install my app into my phone. Everything works fine including the button, on the emulator.
However, when I try using it on my phone, pressing the button crashes my phone. Floating action button works, but not
I tried commenting out the startActivity functions that are linked to the button, but it still crashes. Any possible reasons why?
Sorry if it is a bit vague, but I am not sure what the root of the problem is.
I tried setting backToMainScreen method to nothing, but the app still crashes, so I don't think it is a problem with the java code.
One of the pages that caused problem
public class AddExercise extends AppCompatActivity {
private ArrayList<Exercise> exList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_exercise);
exList = MainActivity.loadData();
}
void backToMainScreen(View target){
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
MainActivity.saveData(exList);
startActivity(myIntent);
finish();
}
void addExercise(View target){
EditText etNewEntry = (EditText) findViewById(R.id.newEntry);
String newEntry = etNewEntry.getText().toString();
if (!newEntry.isEmpty()) {
Exercise newExercise = new Exercise(newEntry);
exList.add(newExercise);
}
backToMainScreen(target);
}
}
Here is the XML code for that page
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/popupWindow">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/exInput"
android:layout_above="@+id/newEntry"
android:gravity="center"/>
<EditText
android:layout_width="200sp"
android:layout_height="wrap_content"
android:id="@+id/newEntry"
android:layout_above="@+id/buttons"
android:layout_alignLeft="@+id/buttons"
android:layout_alignStart="@+id/buttons"
android:inputType="textAutoComplete"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttons"
android:layout_centerInParent="true"
android:orientation="horizontal">
<Button
android:layout_width="100sp"
android:layout_height="wrap_content"
android:id="@+id/ok"
android:text="@string/ok"
android:onClick="addExercise"
/>
<Button
android:layout_width="100sp"
android:layout_height="wrap_content"
android:id="@+id/cancel"
android:text="@string/cancel"
android:onClick="backToMainScreen"
/>
</LinearLayout>
</RelativeLayout>
In order for android:onClick
to work your target function should be public
.
public void backToMainScreen(View target){}
public void addExercise(View target){}
In order for this to work, the method must be public and accept a View as its only parameter. Please make your methods public in your code for the button click.
Read the documentation https://developer.android.com/reference/android/widget/Button.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.