- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为大学的一门课开发一个简单的观鸟日志应用程序,但我似乎无法发现到底出了什么问题,所以在花了几天时间把头撞到墙上后,我想,让有一双新眼睛的人来看看会更有意义。
该应用程序非常简单 - 我有两个 Activity ,一个带有 ListView,由数据库填充(MainActivity),另一个则采用参数来创建新条目(AddActivity)。除了这些之外,我还有一个 DBHelper (MySQLiteHelper) 来处理我的数据库调用,一个简单的类来描述我的对象应该是什么样子 (Sighting),一个数据源类 (SightingDataSource) 来处理促进对帮助程序的调用以及一堆不相关的资源(布局等)。AddActivity 在调用 addSighting 方法时会因空指针异常而崩溃。无论出于何种原因,它也不喜欢 bCount 字段的 int 类型转换,因此出于测试目的,我设置了一个静态 int,同时我找出了问题的异常部分。
这是我的来源:添加 Activity :
package com.vladislavtachev.cscb763;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends ListActivity {
public SightingsDataSource datasource;
private EditText locFld = null;
private EditText bNameFld = null;
private EditText bCountFld = null;
private Button addSightBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
locFld = (EditText) findViewById(R.id.locFld);
bNameFld = (EditText) findViewById(R.id.bNameFld);
bCountFld = (EditText) findViewById(R.id.bCountFld);
setContentView(R.layout.activity_add);
addSightBtn = (Button) findViewById(R.id.addSightBtn);
addSightBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addSight(v);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item);
}
public void addSight(View view){
long time = System.currentTimeMillis()/1000L;
datasource.addSighting(locFld.getText().toString(),
time,
bNameFld.getText().toString(),3
// Integer.parseInt(bCountFld.getText().toString())
);
// datasource.addSighting(sighting);
setContentView(R.layout.activity_main);
}
}
SightingDataSource
package com.vladislavtachev.cscb763;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class SightingsDataSource {
//DB info
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COL_ID, MySQLiteHelper.COL_LOCATION,
MySQLiteHelper.COL_TIME, MySQLiteHelper.COL_BNAME, MySQLiteHelper.COL_BCOUNT };
public SightingsDataSource(Context context){
dbHelper = new MySQLiteHelper(context);
}
public void open(){
database = dbHelper.getWritableDatabase();
}
public void close(){ dbHelper.close(); }
public void addSighting(String l, long time, String bName, int bCount){
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COL_LOCATION, l);
values.put(MySQLiteHelper.COL_TIME, time);
values.put(MySQLiteHelper.COL_BNAME, bName);
values.put(MySQLiteHelper.COL_BCOUNT, bCount);
long insertId = database.insert(MySQLiteHelper.TABLE_SIGHTINGS, null, values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_SIGHTINGS, allColumns, MySQLiteHelper.COL_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Sighting newSighting = cursorToSighting(cursor);
cursor.close();
}
private Sighting cursorToSighting(Cursor cursor){
Sighting sighting = new Sighting();
sighting.setId(cursor.getLong(0));
sighting.setLocation(cursor.getString(1));
sighting.setTime(cursor.getLong(2));
sighting.setbName(cursor.getString(3));
sighting.setbCount(cursor.getInt(4));
return sighting;
}
public List<Sighting> getAllSightings(){
List<Sighting> sightings = new ArrayList<Sighting>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_SIGHTINGS, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Sighting sighting = cursorToSighting(cursor);
sightings.add(sighting);
cursor.moveToNext();
}
cursor.close();
return sightings;
}
}
MySQLiteHelper
package com.vladislavtachev.cscb763;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
public class MySQLiteHelper extends SQLiteOpenHelper{
public final static String DB_NAME = "sightings.db";
public final static int DB_VER = 1;
public final static String TABLE_SIGHTINGS = "sightings";
public final static String COL_ID = "_id";
public final static String COL_LOCATION = "_loc";
public final static String COL_TIME = "_time";
public final static String COL_BNAME = "_bname";
public final static String COL_BCOUNT = "_bcount";
private final static String DB_CREATE = "create table if not exists"
+ TABLE_SIGHTINGS + "("
+ COL_ID + " integer primary key autoincrement, "
+ COL_LOCATION + " text not null, "
+ COL_TIME + " integer not null, "
+ COL_BNAME + " text not null, "
+ COL_BCOUNT + " integer not null);";
public MySQLiteHelper(Context context){
super(context, DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase database){
database.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVer, int newVer){
database.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGHTINGS);
onCreate(database);
}
}
activity_add.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.vladislavtachev.cscb763.AddActivity"
style="@android:style/Theme.Material">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/lblBName"
android:id="@+id/lblBName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bNameFld"
android:layout_below="@+id/lblBName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/lblBCount"
android:id="@+id/lblBCount"
android:layout_alignBottom="@+id/bCountFld"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/bCountFld"
android:layout_below="@+id/bNameFld"
android:layout_alignRight="@+id/bNameFld"
android:layout_alignEnd="@+id/bNameFld"
android:layout_toEndOf="@+id/lblBCount"
android:layout_toRightOf="@+id/lblBCount" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/lblLocation"
android:id="@+id/lblLocation"
android:layout_alignBottom="@+id/locFld"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/locFld"
android:layout_below="@+id/bCountFld"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/lblLocation"
android:layout_toRightOf="@+id/lblLocation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addSightBtn"
android:id="@+id/addSightBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<ListView
android:layout_width="10px"
android:layout_height="10px"
android:id="@android:id/list"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/locFld"
android:layout_alignEnd="@+id/locFld" />
</RelativeLayout>
错误堆栈
07-01 18:08:49.131 2384-2384/com.vladislavtachev.cscb763 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vladislavtachev.cscb763, PID: 2384
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.vladislavtachev.cscb763.SightingsDataSource.addSighting(java.lang.String, long, java.lang.String, int)' on a null object reference
at com.vladislavtachev.cscb763.AddActivity.addSight(AddActivity.java:75)
at com.vladislavtachev.cscb763.AddActivity$1.onClick(AddActivity.java:38)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
感谢所有帮助!
干杯,五、/
最佳答案
datasource
从未在 AddActivity
中初始化。您还调用 setContentView
两次。删除第二个。
关于java - 添加到数据库时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172551/
我创建了一个用户可以添加测试的字段。这一切运行顺利我只希望当用户点击(添加另一个测试)然后上一个(添加另一个测试)删除并且这个显示在新字段中。 所有运行良好的唯一问题是点击(添加另一个字段)之前添加另
String[] option = {"Adlawan", "Angeles", "Arreza", "Benenoso", "Bermas", "Brebant
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在努力将 jQuery 滚动功能添加到 nav-tab (Bootstrap 3)。我希望用户能够选择他们想要的选项卡,并在选项卡内容中有一个可以平滑滚动到 anchor 的链接。这是我的代码,可
我正在尝试在用户登录后再添加 2 个 ui 选项卡。首先,我尝试做一个之后。 $('#slideshow').tabs('remove', '4'); $("#slideshow ul li:last
我有一个包含选择元素的表单,我想通过选择添加和删除其中一些元素。这是html代码(这里也有jsfiddle http://jsfiddle.net/txhajy2w/):
正在写这个: view.backgroundColor = UIColor.white.withAlphaComponent(0.9) 等同于: view.backgroundColor = UICo
好的,如果其中有任何信息,我想将这些列添加到一起。所以说我有 账户 1 2 3 . 有 4 个帐户空间,但只有 3 个帐户。我如何创建 java 脚本来添加它。 最佳答案 Live Example H
我想知道是否有一种有效的预制算法来确定一组数字的和/差是否可以等于不同的数字。示例: 5、8、10、2,使用 + 或 - 等于 9。5 - 8 = -3 + 10 = 7 + 2 = 9 如果有一个预
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我的 Oracle SQL 查询如下- Q1- select hca.account_number, hca.attribute3, SUM(rcl.extended_amou
我正在阅读 http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingG
我正在尝试添加一个“加载更多”按钮并限制下面的结果,这样投资组合页面中就不会同时加载 1000 个内容,如下所示:http://typesetdesign.com/portfolio/ 我对 PHP
我遇到这个问题,我添加了 8 个文本框,它工作正常,但是当我添加更多文本框(如 16 个文本框)时,它不会添加最后一个文本框。有人遇到过这个问题吗?提前致谢。 Live Link: JAVASCRIP
add/remove clone first row default not delete 添加/删除克隆第一行默认不删除&并获取正确的SrNo(例如:添加3行并在看到问题后删除SrNo.2)
我编码this ,但删除按钮不起作用。我在控制台中没有任何错误.. var counter = 0; var dataList = document.getElementById('materi
我有一个类似数组的对象: [1:数组[10]、2:数组[2]、3:数组[2]、4:数组[2]、5:数组[3]、6:数组[1]] 我正在尝试删除前两个元素,执行一些操作,然后将它们再次插入到同一位置。
使用的 Delphi 版本:2007 你好, 我有一个 Tecord 数组 TInfo = Record Name : String; Price : Integer; end; var Info
我使用了基本的 gridster 代码,然后我声明了通过按钮添加和删除小部件的函数它工作正常但是当我将调整大小功能添加到上面的代码中时,它都不起作用(我的意思是调整大小,添加和删除小部件) 我的js代
title 323 323 323 title 323 323 323 title 323 323 323 JS $(document).keydown(function(e){
我是一名优秀的程序员,十分优秀!