- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看到行程和人员详细信息保留在数据库中,然后应用程序崩溃了。向数据库插入数据后不返回MainActivity.java
03-29 21:19:50.277: E/Trace(1288): error opening trace file: No such file or directory (2)
03-29 21:20:34.477: E/AndroidRuntime(1288): FATAL EXCEPTION: main
03-29 21:20:34.477: E/AndroidRuntime(1288): java.lang.IllegalStateException: Could not execute method of the activity
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.view.View$1.onClick(View.java:3599)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.view.View.performClick(View.java:4204)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.view.View$PerformClick.run(View.java:17355)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.os.Handler.handleCallback(Handler.java:725)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.os.Handler.dispatchMessage(Handler.java:92)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.os.Looper.loop(Looper.java:137)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-29 21:20:34.477: E/AndroidRuntime(1288): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:20:34.477: E/AndroidRuntime(1288): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 21:20:34.477: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-29 21:20:34.477: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-29 21:20:34.477: E/AndroidRuntime(1288): at dalvik.system.NativeStart.main(Native Method)
03-29 21:20:34.477: E/AndroidRuntime(1288): Caused by: java.lang.reflect.InvocationTargetException
03-29 21:20:34.477: E/AndroidRuntime(1288): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:20:34.477: E/AndroidRuntime(1288): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 21:20:34.477: E/AndroidRuntime(1288): at android.view.View$1.onClick(View.java:3594)
03-29 21:20:34.477: E/AndroidRuntime(1288): ... 11 more
03-29 21:20:34.477: E/AndroidRuntime(1288): Caused by: java.lang.NullPointerException
03-29 21:20:34.477: E/AndroidRuntime(1288): at com.example.assignment1.controller.CreateTripActivity.persistTrip(CreateTripActivity.java:104)
03-29 21:20:34.477: E/AndroidRuntime(1288): at com.example.assignment1.controller.CreateTripActivity.createTrip(CreateTripActivity.java:82)
03-29 21:20:34.477: E/AndroidRuntime(1288): ... 14 more
03-29 21:25:36.305: E/Trace(1756): error opening trace file: No such file or directory (2)
CreateTripActivity.java
package com.example.assignment1.controller;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import com.example.assignment1.R;
import com.example.assignment1.model.Person;
import com.example.assignment1.model.Trip;
public class CreateTripActivity extends Activity {
static final String TRIP_KEY="TRIP";
static final String LOG="LOG";
private static final int REQUEST_CONTACT = 2;
Person[] person=new Person[10];
int i=0;
String plist="";
EditText name;
EditText place;
EditText date;
EditText starttime;
EditText endtime;
EditText list;
EditText description;
Calendar c = Calendar.getInstance();
@Ghi đè
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createtrip);
name=(EditText)findViewById(R.id.nametext);
place=(EditText)findViewById(R.id.placetext);
date=(EditText)findViewById(R.id.datetext);
starttime=(EditText)findViewById(R.id.starttimetext);
endtime=(EditText)findViewById(R.id.endtimetext);
description=(EditText)findViewById(R.id.descriptiontext);
list=(EditText)findViewById(R.id.personlist);
// handling incoming google maps intent
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
/**
* This method should be used to
* instantiate a Trip model.
*
* @return The Trip as represented
* by the View.
*/
public Trip createTrip(View v) {
String tripname=name.getText().toString();
String tripplace=place.getText().toString();
String tripdate=date.getText().toString();
String tripstarttime=starttime.getText().toString();
String tripendtime=endtime.getText().toString();
String tripdescription=description.getText().toString();
Trip trip=new Trip(tripname,tripdate,tripstarttime,tripendtime,tripplace,tripdescription,c.getTime().toString());
persistTrip(trip,person);
Log.i("LOG","returning to main");
Intent i=new Intent(this,MainActivity.class);
startActivity(i);
trả về giá trị null;
}
//persist person
public void persistPerson(String name, String phone){
person[i] =new Person(name,phone);
Log.i(LOG,person[i].getName());
i +=1;
}
/*
persist trip to database
*/
public void persistTrip(Trip trip,Person[] person) {
DBAdapter db=new DBAdapter(this);
db.open();
db.insertTrip(trip.getName(),trip.getPlace(), trip.getDate(), trip.getStartTime(),trip.getEndTime(),trip.getDescription(),trip.getCreatetime());
for(i=0;i<>
{
Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());
db.insertPerson(person[i].getName(),person[i].getPhone());
}
db.close();
}
/**
* This method should be used when a
* user wants to cancel the creation of
* a Trip.
*
* Note: You most likely want to call this
* if your activity dies during the process
* of a trip creation or if a cancel/back
* button event occurs. Should return to
* the previous activity without a result
* using finish() and setResult().
*/
public void cancelTripCreation(View v) {
Intent back=new Intent(this,MainActivity.class);
setResult(RESULT_OK,back);
finish();
}
//Datepickerlistener
DatePickerDialog.OnDateSetListener datepick = new DatePickerDialog.OnDateSetListener() {
@Ghi đè
public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth ) {
c.set( Calendar.YEAR, year );
c.set( Calendar.MONTH, monthOfYear );
c.set( Calendar.DAY_OF_MONTH, dayOfMonth );
setCurrentDateOnView();
}
};
//Date picker
public void dateOnClick( View view ) {
Log.i(LOG,"Date clicked");
new DatePickerDialog(CreateTripActivity.this, datepick,
c.get( Calendar.YEAR ), c.get( Calendar.MONTH ), c.get( Calendar.DAY_OF_MONTH ) ).show();
}
//listener for timepickerdialog
TimePickerDialog.OnTimeSetListener timepick = new TimePickerDialog.OnTimeSetListener() {
@Ghi đè
public void onTimeSet( TimePicker view, int hourOfDay, int minute ) {
c.set( Calendar.HOUR_OF_DAY, hourOfDay );
c.set( Calendar.MINUTE, minute );
setCurrentDateOnView();
}
};
//time picker
public void timeOnClick( View view ) {
new TimePickerDialog( CreateTripActivity.this, timepick,
c.get( Calendar.HOUR ), c.get( Calendar.MINUTE ), false ).show();
}
public void setCurrentDateOnView() {
String dateFormat = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat( dateFormat, Locale.US );
date.setText( sdf.format( c.getTime() ) );
String timeFormat = "hh:mm a";
SimpleDateFormat stf = new SimpleDateFormat( timeFormat, Locale.US );
starttime.setText( stf.format( c.getTime() ) );
}
//listener for timepickerdialog
TimePickerDialog.OnTimeSetListener endtimepick = new TimePickerDialog.OnTimeSetListener() {
@Ghi đè
public void onTimeSet( TimePicker view, int hourOfDay, int minute ) {
c.set( Calendar.HOUR_OF_DAY, hourOfDay );
c.set( Calendar.MINUTE, minute );
setCurrentDateOnView1();
}
};
//time picker
public void endtimeOnClick( View view ) {
new TimePickerDialog( CreateTripActivity.this, endtimepick,
c.get( Calendar.HOUR ), c.get( Calendar.MINUTE ), false ).show();
}
public void setCurrentDateOnView1() {
String dateFormat = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat( dateFormat, Locale.US );
date.setText( sdf.format( c.getTime() ) );
String timeFormat = "hh:mm a";
SimpleDateFormat stf = new SimpleDateFormat( timeFormat, Locale.US );
endtime.setText( stf.format( c.getTime() ) );
}
// add persons to trip via phonebook
public void contacts(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, REQUEST_CONTACT);
Log.i(LOG,"went");
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CONTACT) {
Uri uri = data.getData();
String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME
// ContactsContract.CommonDataKinds.Phone.NUMBER
};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
//int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
LÀM {
String name = people.getString(indexName);
String number = null;
persistPerson(name,number);
plist +=name+"\n";
// plist +=number;
} while (people.moveToNext());
}
list.setText(plist);
}
public void googleMaps(View v){
// Build the intent
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
// Verify it resolves
PackageManager packageManager = getPackageManager();
List activities = packageManager.
queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {startActivity(mapIntent);}
}
//handling incoming intent from google map
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
place.setText(sharedText);
}
}
}
DBAdapter.java
// ------------------------------------ DBADapter.java ---------------------------------------------
// TODO: Change the package to match your project.
package com.example.assignment1.controller;
import com.example.assignment1.model.Person;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
// For logging:
private static final String TAG = "DBAdapter";
// Trip table Fields
public static final String TABLE_NAME = "trip";
public static final String TRIP_ID = "_id";
public static final String TRIP_NAME = "tripname";
public static final String TRIP_LOCATION = "tripplace";
public static final String TRIP_DATE = "tripdate";
public static final String TRIP_START = "tripstarttime";
public static final String TRIP_END = "tripendtime";
public static final String TRIP_CREATED = "tripcreatetime";
public static final String TRIP_DESCRIPTION = "tripdescription";
//Person Table Fields
public static final String PERSON_TABLE = "person";
public static final String PERSON_ID = "P_id";
public static final String PERSON_NAME = "personname";
public static final String PERSON_NUMBER = "personnumber";
private static final String DATABASE_NAME = "tripplanner.db";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table " + TABLE_NAME +
"(" +TRIP_ID+ " integer primary key autoincrement, "
+ TRIP_NAME+ " text not null, "
+ TRIP_LOCATION+ " text not null, "
+ TRIP_DATE+ " text not null, "
+ TRIP_START+ " text not null, "
+ TRIP_END+ " text not null, "
+ TRIP_CREATED+ " text not null, "
+ TRIP_DESCRIPTION+ " text"
+ ");";
private static final String PERSON_CREATE = "create table " + PERSON_TABLE +
"(" +TRIP_ID+ " integer not null, "
+ PERSON_ID+ " integer primary key autoincrement, "
+ PERSON_NAME+ " text not null, "
+ PERSON_NUMBER+ " text, "
+ " FOREIGN KEY ("+TRIP_ID+") REFERENCES "+TABLE_NAME+" ("+TRIP_ID+")"
+ ");";
// Context of application who uses us.
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
// Public methods:
public DBAdapter(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new trip attributes to database.
public void insertTrip(String tripname,String location,String tripdate,String tripstarttime, String tripendtime,String tripdescription,String tripcreatetime) {
// Create row's data:
ContentValues initialValues = new ContentValues();
initialValues.put(TRIP_NAME, tripname);
initialValues.put(TRIP_LOCATION,location);
initialValues.put(TRIP_START,tripstarttime);
initialValues.put(TRIP_END,tripendtime);
initialValues.put(TRIP_DATE,tripdate);
initialValues.put(TRIP_CREATED,tripcreatetime);
initialValues.put(TRIP_DESCRIPTION,tripdescription);
// Insert it into the database.
db.insert(TABLE_NAME, null, initialValues);
Log.i(TAG,"insert into"+TABLE_NAME+"values"+tripname+","+location+","+tripdate+","+tripstarttime+","+tripendtime+","+tripdescription+","+tripcreatetime);
return;
}
// Add a new person attributes to database.
public void insertPerson(String personname,String personnumber) {
// Create row's data:
Log.i(TAG,"inserting person....");
ContentValues initialValues = new ContentValues();
initialValues.put(PERSON_NAME,personname);
initialValues.put(PERSON_NUMBER,personnumber);
Log.i("LOG","fetching max tripid");
initialValues.put(TRIP_ID,getTripId());
Log.i("LOG","max tripid fetched");
// Insert it into the database.
db.insert(PERSON_TABLE, null, initialValues);
Log.i(TAG,"insert into"+PERSON_TABLE+"values"+","+getTripId()+personname+","+personnumber);
return;
}
public String getTripId(){
Log.i("LOG","gettripid");
Cursor cur=db.rawQuery("select max(_id) from trip",null);
if(cur.moveToFirst()){
String tid=cur.getString(0);
Log.i("LOG",tid);
}
return cur.getString(0);
}
/*// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
LÀM {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
*/
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true,TABLE_NAME,null,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = TRIP_ID + "=" + rowId;
Cursor c = db.query(true, TABLE_NAME, null,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
/*
Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, int studentNum, String favColour) {
String where = KEY_ROWID + "=" + rowId;
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues newValues = new ContentValues();
newValues.put(KEY_NAME, name);
newValues.put(KEY_STUDENTNUM, studentNum);
newValues.put(KEY_FAVCOLOUR, favColour);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
*/
/**
* Private class which handles database creation and upgrading.
* Used to handle low-level database access.
*/
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Ghi đè
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(PERSON_CREATE);
_db.execSQL(DATABASE_CREATE);
Log.i(TAG,DATABASE_CREATE);
}
@Ghi đè
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME);
// Recreate new database:
onCreate(_db);
}
}
}
1 Câu trả lời
您已经初始化了 person 数组
Person[] person = new Person[10];
但是,在 persistTrip 方法中,您迭代此数组,并且第一个元素 (person[0]) 为 null,因此该行出现 NPE
Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());
这是因为对象的默认值为 null。
要正确初始化 Person 对象数组,您应该使用:
for (int i = 0; i < 10; i++) {
person[i] = new Person();
}
或者通过空检查包装数组的使用。
关于java.lang.IllegalStateException : Could not execute method of the activity , 由 : java. lang.NullPointerException 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737330/
我有一个坦克射击弹药的游戏。我对这部分代码的目标是检查它们是否与“碰撞”磁贴发生碰撞,如果是,则将其和磁贴移除。 代码如下所示,每 1/60 秒检查一次: Iterator iterator = sh
我尝试使用 JSR-303 注释(类级别)和验证器实现为 play 2.0.1 编写自定义表单验证器。 不幸的是,当我提交表单并且验证失败时,我收到了一个 IllegalStateException,
根据answer of BalusC ,我用过 FacesContext.getCurrentInstance().getExternalContext().redirect(url); 在我的 @P
这个问题已经有答案了: Copy a stream to avoid "stream has already been operated upon or closed" (10 个回答) 已关闭 5
这个问题已经有答案了: Spring: getOutputStream() has already been called for this response (3 个回答) 已关闭 4 年前。 我正
我正在尝试将 Activity 转换为 FragmentActivty 对象,以便获得 FragmentManager 对象 public class Main extends ListActivit
我正在尝试使用可编辑的组合框,通过用户的某些击键从数据库中快速搜索客户端的功能。我想要的是,用户将输入一些字母,如果这些字母与某些客户端匹配,这些客户端将保留在组合框的当前数据模型中。 代码如下。请修
这个问题已经有答案了: You need to use a Theme.AppCompat theme (or descendant) with this activity. Change to Th
我正在使用 Android Studio 和 Genymotion 作为模拟器创建一个应用程序,其中我在 3 个 EditText 中输入数据,当我单击按钮将其存储在 sqlite 数据库中时,它不起
我正在为 Android 构建一个简单的消息应用程序,并且在发送短信时遇到一些问题。我第一次使用 OnlickListener 时,消息被发送并显示在我的 ListView 中。当我在 Activit
我了解到 collect() 和 forEach() 都是流终端操作,在同一个流上调用它们会抛出 非法状态异常。但是,以下代码可以成功编译并按升序打印每个字符串的长度。不会引发任何异常。怎么会这样?
我对 classcastException 和非法状态异常都有点困惑,因为在大多数情况下它们看起来都很相似。 我在这个java代码中遇到了一个问题 class consumer {
我正在尝试这个小计算器程序。当我调用calculateResult()方法时,我想在第二个操作数为零且运算符为除法时显示IllegalStateException错误。但尽管如此,我在calculat
Stacktrace Here 导入java.util.*; 公共(public)类 AccountClient { public static void main(String[] args) {
我正在使用 readEntity() 方法读取 JAVAX 响应,但我收到以下堆栈跟踪: java.lang.IllegalStateException: Entity input stream ha
我是安卓新手。我正在尝试进行简单的登录 Activity ,但当我单击“登录”按钮时出现运行时错误。我认为我没有正确获取数据。我已经检查过,SQLite 中有一个与该 PK 相对应的数据。 日志猫。
我正在创建一个登录页面,工程师可以通过以“engg”开头的用户名登录。问题出在登录页面,当我使用正确的密码提供正确的输入时,它会给出“非法状态异常”。在错误的输入中,它工作正常。就像当我在我的 ora
我正在使用一些现有的 Java 设备驱动程序软件,该软件使用 JavaCOMM 进行串行 I/O。我昨天看到它抛出一个异常,其中有一个 IllegalStateException - 端口从 publ
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在使用 Adventnet SNMPAPI 开发 UDP 监听程序。现在我需要将监听数据保存到数据库中。当我这样做时,我遇到了错误。任何人都可以帮忙解决这个问题吗... 这是我的代码。 impor
Tôi là một lập trình viên xuất sắc, rất giỏi!