- 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/
我正在尝试从par_iter()内部捕捉到 panic ,并继续执行par_iter块之后的操作。 如果我有这个,我会正确地获得一切,并且不会出现 panic : let dog: Dog = Dog
我可以假设从 JDK 类加载机制抛出的每个 NoClassDefFoundError 总是在堆栈跟踪中将 ClassNotFoundException 作为原因吗? 另外,NoClassDefFoun
我有下面的程序 package com; import java.io.PrintStream; import java.net.URL; import java.net.UR
我有一些由另一组人编写的简单代码(目前不可用),它引用了我得到的一些 jar 文件。当我编译代码时,一切都构建得很好,但是一旦代码尝试创建在其中一个 jar 文件中定义的类的实例,我就会收到 java
我正在尝试按照 https://github.com/airbrake/airbrake-django#manually-sending-errors-to-airbrake 上的示例进行操作手动向
我不确定为什么这是递归的。 jTable1.getModel().addTableModelListener(new TableModelListener() { public void table
我按照 https://github.com/cloudfoundry/vcap 上的自述文件进行操作 它应该工作正常... 但我得到了这样的错误: 有谁知道发生了什么? 我在 Ubuntu10.04
我只是想知道当你有 UI-Router 的空白页面时,有人知道如何调试情况。 (当然,控制台没有任何错误) 通过为路由器事件执行 console.log(取自 here),我发现它进入了正确的状态,但
我们的网站上有一个问题,一些 Firefox 用户在访问我们的网站时会收到“错误请求”消息(仅此而已,只是“错误请求”字样!) 这似乎是由于 google 跟踪 cookie 损坏,可能是 __utm
在使用guard-rspec在Rails 4项目中运行guard时,在vim中打开/关闭文件时偶尔会看到以下错误。我试过升级/降级guard,guard-rspec,pry和其他没有运气的库。 rub
今天我在编写程序时遇到了这个错误。 Caused by:java.lang.ClassCastException: org.cubeville.blocks.CrossedBlockBrush can
我在执行应用程序时遇到空指针异常,但我不确定原因。问题发生在线路上: task.execute(""); 但我不确定为什么会出现空指针异常。 (我已经验证我有互联网连接,并且它所连接的 XML
嗨,我有一个 java 应用程序,我正在尝试使用它写入 tempDir,但我仍然遇到以下异常。我承认我对编写文件不太了解,所以希望我缺少一些小东西。 Caused by: java.io.FileNo
我不明白为什么会发生这种情况。我对其他问题做了一些研究,发现使用 for 循环时无法修改集合。但是,我正在使用迭代器,为什么它不起作用? int counter = 0; int otherC
目前我正在使用 OSX Server (Yosemite) 来托管一堆 PHP 应用程序,其中一些应用程序在网站文档根目录下有一个子目录用于子域。自更新到 Yosemite 版本的 OSX Serve
SqlCommand objsql = new SqlCommand(); . . objsql.Parameters.AddWithValue("@Param1", DBNull.Value); .
当我尝试将“对象”添加到数据库然后将其显示到 TableView 时,它显示 UnsupportedOperationException 。一切都很好,直到我将此代码添加到“public void i
我收到以下错误日志: 05-29 20:57:29.886: D/AndroidRuntime(359): Shutting down VM 05-29 20:57:29.896: W/dalvikv
我有两个项目,第一个是Ejb3项目,名称是SessionBean,另一个是java项目,名称是SessionBeanClient。对于 IDE,我使用 eclipse indigo。我已经完成了代码,
我有一个使用表单成员身份验证的 ASP.NET Web 应用程序。我们最近进行了渗透测试,标记的一个问题是窃取用户帐户的能力。如果 .ASPXAUTH cookie 值是在注销之前从用户复制的,用户可
我是一名优秀的程序员,十分优秀!