- 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();
@Override
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);
return 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<person.length;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() {
@Override
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() {
@Override
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() {
@Override
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();
do {
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<ResolveInfo> 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()) {
do {
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);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(PERSON_CREATE);
_db.execSQL(DATABASE_CREATE);
Log.i(TAG,DATABASE_CREATE);
}
@Override
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);
}
}
}
最佳答案
您已经初始化了 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/
我正在将我的模板代码移植到 XTend。在某些时候,我在测试用例中有这种类型的条件处理: @Test def xtendIfTest() { val obj = new FD if (
我是新来的 kotlin , 当我开始 Null Safety 时,我对下面的情况感到困惑. There's some data inconsistency with regard to initia
我的应用程序一直在各种Android版本中保持良好状态。我有用户在Android 4.3、5.0、5.1和6.0上正常运行。但是,具有S7 Edge的用户刚刚更新了Android 7.0,将文本粘贴到
我使用的是最新版本的 LWUIT (1.5)。我在资源编辑器中设计了我的表单,然后将代码生成到 netbeans。问题是如果我想访问除表单之外的任何对象,我会收到此错误: java.lang.Null
更新: 我在 Fedora 21 上运行它。 SonarQube - 5.0。 SonarQube Runner - 2.4 更新 2:Findbugs v3.1,Java 插件 v2.8 更新3:
RecupData 我的类仅在 web 中返回 NullPointerException。我连接到 pgsql db 8.3.7 - 该脚本在“控制台”syso 中运行良好 - 但引发了测试 Web
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
每次运行此 jsp 时,都会收到以下错误异常: org.apache.jasper.JasperException: java.lang.NullPointerException root cause
Kotlin 在编译时有一个出色的 null 检查,使用分离到“可空?”和“不可为空”的对象。它有一个 KAnnotator 来帮助确定来自 Java 的对象是否可以为空。但是,如果 not-null
我有一个布局将显示一个TextView,用于显示一个滴答时间。我遵循了此链接中的代码 How to Display current time that changes dynamically for
Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”) 我有一个像这样的文件: $ curl 'http://localhost:9200/blog/artic
这是我从另一个类调用函数的方法Selenium 设置已定义。 public void Transfer() throws Exception { System.out.println("\nTrans
我试图在主类中使用我在此类中创建的函数,但它崩溃并显示“警告:无法在根 0 处打开/创建首选项根节点 Software\JavaSoft\Prefsx80000002。 Windows RegCrea
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 3 年前。 我有一个 Java 代码,它将
我声明了两张牌: Card card1 = new Card('3', Card.Suit.clubs); Card card2 = new Card('T', Card.Suit.diamonds)
我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lan
我正在尝试使用 arrayList 的 arrayList 在 Java 中实现图形。 每当调用 addEdge 函数时,我都会收到 NullPointerException 。我似乎无法弄清楚为什么
我是 Java/android 的新手,所以很多这些术语都是外国的,但我愿意学习。我不打算详细介绍该应用程序,因为我认为它不相关。我目前的问题是,我使用了博客中的教程和代码 fragment ,并使我
我正在开发一个 Android 应用程序来在 Android developer guide 的帮助下录制视频.我程序上的所有代码都与此页面相同。 我在 之外定义了权限标签。 当应
我是一名优秀的程序员,十分优秀!