gpt4 book ai didi

android - 为什么我的Android应用程序中出现 “unfortunately myapp has stopped”?

转载 作者:行者123 更新时间:2023-12-03 08:35:20 25 4
gpt4 key购买 nike

在我的应用程序中单击图像按钮时,我总是收到消息:“不幸的是,myapp已停止”。问题是什么???

这是我的代码。单击图像按钮将调用showImageDialog方法:

public class ReminderEditActivity extends Activity {

//
// Private instance variables
//
private Button mDateButton;
private Calendar mCalendar;
private Button mTimeButton;
private EditText mTitleText;
private EditText mBodyText;
private Button mConfirmButton;

private RemindersDbAdapter mDbHelper;
private ImageAdapter mImageHelper;

private Long mRowId;
private Long mImageRowId;

private ImageButton mImageButton;

private Uri mImageCaptureUri;

public String imageFilePath;


//
// Private constants
//
private static final int DATE_PICKER_DIALOG = 0;
private static final int TIME_PICKER_DIALOG = 1;
private static final String DATE_FORMAT = "dd-MM-yyyy";
private static final String TIME_FORMAT = "kk:mm";

public static final String DATE_TIME_FORMAT = "dd-MM-yyyy kk:mm:ss";

private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_FILE = 2;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Instantiate mDbHelper and mImageHelper
mDbHelper = new RemindersDbAdapter(this);
mImageHelper = new ImageAdapter(this);

setContentView(R.layout.reminder_edit);

//
// Instantiate instance variables
//
mTimeButton = (Button)findViewById(R.id.reminder_time);
mCalendar = Calendar.getInstance();
mDateButton = (Button)findViewById(R.id.reminder_date);
mConfirmButton = (Button)findViewById(R.id.confirm);
mTitleText = (EditText)findViewById(R.id.title);
mBodyText = (EditText)findViewById(R.id.body);

mImageButton =(ImageButton)findViewById(R.id.image);

//Checks whether mRowId contains any values.
mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID) : null;
mImageRowId = savedInstanceState != null ? savedInstanceState.getLong(ImageAdapter.KEY_ROWID) : null;

registerButtonListenerandSetDefaultText();
}

//
// Sets the row id from the intent. If mRowId is null its content is null.
//
private void setRowIdFromIntent() {
if(mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(RemindersDbAdapter.KEY_ROWID) : null;
}
}

//
// Executed on pause.
//
@Override
protected void onPause() {
super.onPause();
mDbHelper.close();
mImageHelper.close();
}

//
// Executed on resume
//
@Override
protected void onResume() {
super.onResume();
mDbHelper.open();
mImageHelper.open();
setRowIdFromIntent();
populateFields();
populateImageField();
}

//
// Register the Date, Time and Save Button and set the default text
//
private void registerButtonListenerandSetDefaultText() {

mDateButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
showDialog(DATE_PICKER_DIALOG);
}
});

mTimeButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});

mConfirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveState();
setResult(RESULT_OK);
Toast.makeText(ReminderEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();
}
});

mImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showImageDialog();
}
});

updateDateButtonText();
updateTimeButtonText();
}


private void showImageDialog() {
final String [] items = new String [] {"From Camera", "From SD Card"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);

try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);

startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}

dialog.cancel();
} else {
Intent intent = new Intent();

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );

final AlertDialog dialog = builder.create();
dialog.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_FROM_FILE && data != null && data.getData() != null) {
Uri uri = data.getData();
if(uri != null) {
Cursor cursor = getContentResolver().query(uri, new String[] {
android.provider.MediaStore.Images.ImageColumns.DATA},
null, null, null);
cursor.moveToFirst();
String imageFilePath = cursor.getString(0);
cursor.close();

if(imageFilePath != null) {
// Do whatever you want like decode it into a Bitmap
Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
mImageButton.setImageBitmap(bitmap);
// Or.. store it somewhere in your local db
}
}
}
}


//
// If mRowId is not null (it contains value and we want to edit the task)
// this method is called.
//
private void populateFields() {
if (mRowId != null) {
Cursor reminder = mDbHelper.fetchReminder(mRowId);
startManagingCursor(reminder);
mTitleText.setText(reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_TITLE)));
mBodyText.setText(reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_BODY)));


SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
Date date = null;
try {
String dateString = reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_DATE_TIME));
date = dateTimeFormat.parse(dateString);
mCalendar.setTime(date);
} catch (ParseException e) {
Log.e("ReminderEditActivity", e.getMessage(), e);
}
}

updateDateButtonText();
updateTimeButtonText();
}

private void populateImageField() {
if (mImageRowId != null) {
Cursor reminderImage = mImageHelper.fetchReminder(mImageRowId);
startManagingCursor(reminderImage);
String imagePath = reminderImage.getString(reminderImage.getColumnIndexOrThrow(ImageAdapter.KEY_IMAGE));

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
mImageButton.setImageBitmap(bitmap);

}
}


//
// Saves the state so that it can be restored.
//
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);

}

//
// Saves the content by communicating with the RemindersDbAdapter.
//
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();

SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());

// Creates a new reminder if the value is null.
if (mRowId == null) {
long id = mDbHelper.createReminder(title, body, reminderDateTime);
if (id > 0) {
mRowId = id;

}
// Updates the reminder if the content is not null.
} else {
mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
}
}

//
// Opens up an alert dialog box and asks the user if he wants to discard his task.
//
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(ReminderEditActivity.this);
builder.setMessage("Are you sure you want to discard your task?");
builder.setTitle("Are you sure?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int id) {
Intent j = new Intent(((Dialog)dialog).getContext(), ReminderListActivity.class);
startActivity(j);
return;

}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();

}
});
builder.create().show();
}

//
// When creating a dialog box, it either shows up a date or time picker.
//
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_DIALOG:
return showDatePicker();
case TIME_PICKER_DIALOG:
return showTimePicker();
}
return super.onCreateDialog(id);
}

//
// Creates the date picker dialog box.
//
private DatePickerDialog showDatePicker() {
DatePickerDialog datePicker = new DatePickerDialog(ReminderEditActivity.this, new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDateButtonText();
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
return datePicker;
}

//
// Creates the time picker dialog box.
//
private TimePickerDialog showTimePicker() {
TimePickerDialog timePicker = new TimePickerDialog(ReminderEditActivity.this, new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true);
return timePicker;
}

//
// Updates the Date Button text.
//
private void updateDateButtonText() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}

//
// Updates the Time Button text.
//
private void updateTimeButtonText() {
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeForButton = timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}

}

这是LogCat:
05-30 22:09:06.726: E/AndroidRuntime(628): FATAL EXCEPTION: main
05-30 22:09:06.726: E/AndroidRuntime(628): java.lang.RuntimeException: Unable to pause activity {com.ndroidstudios.android.taskreminder/com.ndroidstudios.android.taskreminder.ReminderEditActivity}: java.lang.NullPointerException
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2706)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2662)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2640)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.access$800(ActivityThread.java:123)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1154)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.os.Looper.loop(Looper.java:137)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-30 22:09:06.726: E/AndroidRuntime(628): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 22:09:06.726: E/AndroidRuntime(628): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 22:09:06.726: E/AndroidRuntime(628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-30 22:09:06.726: E/AndroidRuntime(628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-30 22:09:06.726: E/AndroidRuntime(628): at dalvik.system.NativeStart.main(Native Method)
05-30 22:09:06.726: E/AndroidRuntime(628): Caused by: java.lang.NullPointerException
05-30 22:09:06.726: E/AndroidRuntime(628): at com.ndroidstudios.android.taskreminder.ReminderEditActivity.onSaveInstanceState(ReminderEditActivity.java:345)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.Activity.performSaveInstanceState(Activity.java:1113)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
05-30 22:09:06.726: E/AndroidRuntime(628): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2688)
05-30 22:09:06.726: E/AndroidRuntime(628): ... 12 more

这是我的 list 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ndroidstudios.android.taskreminder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ReminderListActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ReminderEditActivity"
android:label="@string/app_name" />
<activity
android:name=".ImageAdapter"
android:label="@string/app_name" />
</application>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

最佳答案

第345行的ReminderEditActivity具有空对象。我不确定代码中的哪一行会导致没有行号

关于android - 为什么我的Android应用程序中出现 “unfortunately myapp has stopped”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10823613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com