【发布时间】:2020-06-30 04:03:26
【问题描述】:
这里是activity_main代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="center"
android:textSize="36sp"
android:textColor="@color/colorPrimary"
android:text="To Do"
android:layout_marginTop="45dp"
android:fontFamily="@font/montserratlight"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_marginTop="126dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="50dp"
android:src="@drawable/plussymbol" />
</RelativeLayout>
这是我的 MainActivity Java 代码:
package com.geoff.productivitywatch;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
int clickCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.listView);
final ArrayList<String> todo = new ArrayList<>();
todo.add("Swipe Up");
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.tododesign, todo);
list.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here is where the pop-up box code goes
final EditText todoEditText = new EditText(getApplicationContext());
AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("New Task")
.setMessage("What next?")
.setView(todoEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Add Task to listview
String task = String.valueOf(todoEditText.getText());
todo.add("" + task);
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
}
}
我尝试编写一个方法,在该方法中单击浮动操作按钮会弹出一个对话框警告框,您可以在其中编辑文本,然后将其添加到列表视图中。 Android Studio 在我的代码中没有看到错误,但是当我在我的 android 设备上测试应用程序时,只要单击浮动操作按钮,应用程序就会崩溃。但是,我知道这不是我的设备的问题,因为我之前已经成功测试过很多次,它只是在我添加 AlertDialog 方法时才开始崩溃。我想我可能在上下文方面出了问题,但是我已将它们更改为我能想到的所有变体,但没有帮助。
【问题讨论】:
-
请显示崩溃日志
标签: java android onclick android-alertdialog floating-action-button