sách gpt4 ai đã đi

java.lang.IllegalStateException : Could not execute method of the activity , 由 : java. lang.NullPointerException 引起

In lại 作者:行者123 更新时间:2023-12-01 23:06:01 26 4
mua khóa gpt4 Nike

我看到行程和人员详细信息保留在数据库中,然后应用程序崩溃了。向数据库插入数据后不返回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/

26 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com