我是android学习的新手。使用Android Studio。我的应用名称是Design1
。我正在学习活动,所以创建了一个活动页面,其中有两个用于测试两个事件的按钮,一个是toast
,另一个是Alert dialog box
。吐司工作正常,但是当我点击ALERT
按钮显示警报对话框然后出现一条消息,即“ Design1已停止”。
如果我从activity_events.xml和events.java文件中删除showAlert事件,那么我的工作就完美了。
events.java
package com.example.borntoflirt.design1;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class events extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
}
public void showToast(View v) {
Context context = getApplicationContext(); // OR getBaseContext()
CharSequence text = "Hi Toast";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void showAlert(View v) {
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
builder.setMessage("Write your message here.");
builder.setCancelable(true);
builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
activity_events.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight=".30"
android:background="@drawable/rounded_button"
android:text="Toast"
android:textColor="#000"
android:onClick="showToast"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:text="Alert"
android:textColor="#000"
android:background="@drawable/rounded_button"
android:layout_margin="2dp"
android:onClick="showAlert"/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
您需要使用活动上下文。如果您使用应用程序上下文,则将抛出IllegalStateExpetion。
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="header">
<div class="header-text">100+ Years Of Combiend Industry Expierence</div>
<div class="header-logo"></div>
</div>
</body>
</html>
答案 1 :(得分:0)
使用活动的背景:
AlertDialog.Builder builder = new AlertDialog.Builder(event.this);
取代
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
答案 2 :(得分:0)
尝试使用getActivity()
代替getApplicationContext()
。
答案 3 :(得分:0)
已经7个月了,但是没有一个答案对我有用。所以这是我找到的解决方案。尝试使用select * from tasks
where initialPrice > 20
and case when status = 3 Then 1 when status in (1,5) and userId = 5 Then 1 else 0 end = 1
而不是MainActivity.this
或getApplicationContext()
。