- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中单击图像按钮时,我总是收到消息:“不幸的是,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);
}
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
<?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/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!