- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我简单地创建了一个在 Google map API 上运行的新模拟器后,以前工作的代码中出现了非常令人沮丧的错误。
代码背景:
当用户点击按钮时,我的代码会检查设备以查看 GPS 当前是否处于 Activity 状态。如果不是,系统会提示用户进入设置页面。如果是,则将它们的 Lat ang Long 值发送到 AsyncTask。
据我所知,我的 GPS 正在返回一个值(无论是真还是假),但我收到以下错误:
03-12 21:33:18.317: E/AndroidRuntime(364): java.lang.RuntimeException: Unable to stop activity {com.example.flybaseapp/com.example.flybaseapp.ShoppingList}: java.lang.NullPointerException
密码:
package com.example.flybaseapp;
import android.R.string;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
public class PassLatLong extends Activity{
double latt;
double longg;
String conLatt = "";
String conLongg = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
latt = extras.getDouble("passedLat");
longg = extras.getDouble("passedLong");
}
conLatt = Double.toString(latt);
conLongg = Double.toString(longg);
GoogleAsync task = new GoogleAsync(this);
task.execute(conLatt, conLongg);
}
}
ShoppingList.Java 调用 Intent 对象启动 CheckGPS 类:
package com.example.flybaseapp;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ShoppingList extends ListActivity implements OnClickListener {
Button AddItem;
Button showShop;
ListView showItems;
SimpleCursorAdapter cursorAdapter;
Long itemId;
EditText totalPrice;
String itemDescription;
int itemAmount;
int itemPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppinglistlayout);
AddItem = (Button) findViewById(R.id.btnAddItem);
showShop = (Button) findViewById(R.id.btnSearchShops);
showItems = (ListView) findViewById(android.R.id.list);
totalPrice = (EditText) findViewById(R.id.totalListPrice);
AddItem.setOnClickListener(this);
showShop.setOnClickListener(this);
setList();
int setPrice = updateTotal();
totalPrice.setText(Integer.toString(setPrice));
}
@Override
public void onClick(View clickedAdd) {
switch (clickedAdd.getId()) {
case (R.id.btnAddItem):
show();
break;
case (R.id.btnSearchShops):
Intent checkGPS = new Intent("com.example.flybaseapp.CheckGPS");
startActivity(checkGPS);
break;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
super.onListItemClick(l, v, position, idd);
itemId = idd;
final CharSequence[] items = { "Edit Item", "Delete Item",
"Show Most Purchased Item" };
Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);
alertDialogBuilder.setTitle("Item Options:");
alertDialogBuilder.setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Edit Item")) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ShoppingList.this);
builder.setTitle("Edit Item");
DBHandlerShop setEdit = new DBHandlerShop(
ShoppingList.this, null, null);
setEdit.open();
String itemName = setEdit.getItem(itemId);
int itemAmount = setEdit.getItemQuan(itemId);
int itemPrice = setEdit.getItemCost(itemId);
setEdit.close();
LinearLayout layout = new LinearLayout(
ShoppingList.this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText titleBox = new EditText(
ShoppingList.this);
titleBox.setText(itemName);
titleBox.setHint("Item Name:");
layout.addView(titleBox);
final EditText quantityBox = new EditText(
ShoppingList.this);
quantityBox.setText(Integer.toString(itemAmount));
quantityBox.setHint("Item Quantity");
layout.addView(quantityBox);
final EditText priceBox = new EditText(
ShoppingList.this);
priceBox.setText(Integer.toString(itemPrice));
priceBox.setHint("Item Price.");
layout.addView(priceBox);
builder.setView(layout);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int whichButton) {
Editable valueItem = titleBox
.getText();
Editable valueAmount = quantityBox
.getText();
Editable valuePrice = priceBox
.getText();
String itemDescription = valueItem
.toString();
String s = valueAmount.toString();
int itemAmount = Integer
.parseInt(s);
String a = valuePrice.toString();
int itemPrice = Integer.parseInt(a);
try {
DBHandlerShop update = new DBHandlerShop(
ShoppingList.this,
null, null);
update.open();
update.updateItem(itemId,
itemDescription,
itemAmount, itemPrice);
update.close();
} catch (Exception e) {
Dialog e1 = new Dialog(
ShoppingList.this);
e1.setTitle("Item unsuccesfully updated");
TextView txt = new TextView(
ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
} finally {
Dialog e1 = new Dialog(
ShoppingList.this);
e1.setTitle("Item succesfully updated");
TextView txt = new TextView(
ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
setList();
int cost = updateTotal();
totalPrice.setText(Integer
.toString(cost));
}
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int whichButton) {
}
});
builder.show();
}
else if (items[item].equals("Delete Item")) {
DBHandlerShop delete = new DBHandlerShop(
ShoppingList.this, null, null);
delete.open();
delete.deleteItem(itemId);
delete.close();
DBHandlerShop findPrice = new DBHandlerShop(
ShoppingList.this, null, null);
findPrice.open();
int returnedCost = findPrice.getItemCost(itemId);
int cost = updateTotal();
int newTotal = cost - returnedCost;
totalPrice.setText(Integer.toString(newTotal));
setList();
}
else if (items[item].equals("Show Most Purchased Item")) {
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item unsuccesfully updated");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
}
}
});
alertDialogBuilder.show();
}
private void setList() {
DBHandlerShop DBShop = new DBHandlerShop(this, null, null);
DBHandlerShop searchItems = new DBHandlerShop(this, null, null);
searchItems.open();
Cursor cursor = searchItems.getItems();
startManagingCursor(cursor);
String[] from = new String[] { DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM,
DBShop.KEY_ITEMPRICE };
int[] to = new int[] { R.id.txtSetItem, R.id.txtSetAmount,
R.id.txtSetPrice };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist,
cursor, from, to);
showItems.setAdapter(cursorAdapter);
}
private int updateTotal() {
DBHandlerShop total = new DBHandlerShop(this, null, null);
int totalPrice = 0;
total.open();
Cursor totalPrices = total.getTotals();
total.close();
if (totalPrices != null) {
startManagingCursor(totalPrices);
if (totalPrices.moveToFirst()) {
do {
int cost = totalPrices.getInt(3);
totalPrice += cost;
} while (totalPrices.moveToNext());
return totalPrice;
}
}
else {
return totalPrice;
}
return 0;
}
private void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);
builder.setTitle("Enter Item Details:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText titleBox = new EditText(this);
titleBox.setHint("Item Name:");
layout.addView(titleBox);
final EditText quantityBox = new EditText(this);
quantityBox.setHint("Item Quantity");
layout.addView(quantityBox);
final EditText priceBox = new EditText(this);
priceBox.setHint("Item Price.");
layout.addView(priceBox);
builder.setView(layout);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
Editable valueItem = titleBox.getText();
Editable valueAmount = quantityBox.getText();
Editable valuePrice = priceBox.getText();
itemDescription = valueItem.toString();
String s = valueAmount.toString();
itemAmount = Integer.parseInt(s);
String a = valuePrice.toString();
itemPrice = Integer.parseInt(a);
int totalCombined = itemAmount * itemPrice;
DBHandlerShop addItem = new DBHandlerShop(
ShoppingList.this, null, null);
addItem.open();
addItem.insertItems(itemDescription, itemAmount,
totalCombined);
addItem.close();
} catch (Exception e) {
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item unsuccesfully added");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
} finally {
Dialog e = new Dialog(ShoppingList.this);
e.setTitle("Item succesfully added.");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e.setContentView(txt);
e.show();
int cost = updateTotal();
totalPrice.setText(Integer.toString(cost));
setList();
}
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
}
这是调用检查的类:
package com.example.flybaseapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
public class CheckGPS extends Activity {
boolean check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GPSYesOrNo g = new GPSYesOrNo(this);
check = g.checkStatus();
if (check == true) {
Intent Appoint = new Intent("com.example.flybaseapp.CurrentLatLong");
startActivity(Appoint);
}
else {
alert();
}
}
private void alert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
还有 GPSYesOrNo 类:
public class GPSYesOrNo{
Context cc;
private LocationManager locationManager;
boolean enable;
public GPSYesOrNo(Context c)
{
this.cc = c;
checkStatus();
}
public boolean checkStatus()
{
locationManager = (LocationManager) cc.getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(enabled){
return true;
}else{
return false;
}
}
异步类:
package com.example.flybaseapp;
import java.util.List;
import com.google.android.maps.MapActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListView;
公共(public)类 GoogleAsync 扩展 AsyncTask> {
private Context context = null;
public GoogleAsync(Context ctx) {
context = ctx;
}
@Override
protected List<JSON> doInBackground(String... arg0) {
return new JSONResponse().searchPostalCode(arg0[0], arg0[1]);
}
@Override
protected void onPostExecute(List<JSON> result) {
this.populateActivity(result);
}
void populateActivity(List<JSON> result) {
// Associate Adapter to ListView for matching locations
MapActivity mapAct = (MapActivity) context;
ListView list = (ListView) mapAct.findViewById(R.id.list);
ShopMapDisplay adapter = new ShopMapDisplay(context, result);
list.setAdapter(adapter);
}
}
完整的 LogCat:
03-12 22:38:56.994: E/AndroidRuntime(372): FATAL EXCEPTION: main
03-12 22:38:56.994: E/AndroidRuntime(372): java.lang.ClassCastException: com.example.flybaseapp.PassLatLong
03-12 22:38:56.994: E/AndroidRuntime(372): at com.example.flybaseapp.GoogleAsync.populateActivity(GoogleAsync.java:32)
03-12 22:38:56.994: E/AndroidRuntime(372): at com.example.flybaseapp.GoogleAsync.onPostExecute(GoogleAsync.java:26)
03-12 22:38:56.994: E/AndroidRuntime(372): at com.example.flybaseapp.GoogleAsync.onPostExecute(GoogleAsync.java:1)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.os.Looper.loop(Looper.java:123)
03-12 22:38:56.994: E/AndroidRuntime(372): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 22:38:56.994: E/AndroidRuntime(372): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 22:38:56.994: E/AndroidRuntime(372): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 22:38:56.994: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 22:38:56.994: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 22:38:56.994: E/AndroidRuntime(372): at dalvik.system.NativeStart.main(Native Method)
目前我的模拟器设置为 GoogleApi 2.2。我真的很困惑这个错误,因为它以前是有效的。在尝试实现 onStop() 覆盖后,这也没有解决问题。
最佳答案
您没有关闭 findPrice.open();
的数据库。没有 findPrice.close();
。
我建议您只在 onCreate()
中打开数据库并在 onDestroy()
中关闭它。这样您就不必一直打开和关闭然后忘记。
关于java - 检查 GPS 状态时无法停止 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372756/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!