我已经尝试了几个小时来弄清楚为什么在加载Profile活动后android崩溃了。引起问题的一行是:
mRowId = extras != null ? extras.getLong(OverLimitDbAdapter.KEY_ROWID): null;
位于Profile.java中。所以我先是
OvertheLimit.java通过菜单选项intent放置额外内容:
package uk.co.obdesign.android.overthelimit;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Overthelimit extends ListActivity {
private OverLimitDbAdapter dbHelper;
private static final int USER_CREATE = 0;
private static final int USER_EDIT = 1;
private Cursor cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getListView();
dbHelper = new OverLimitDbAdapter(this);
dbHelper.open();
fillData();
registerForContextMenu(getListView());
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
private void fillData() {
cursor = dbHelper.fetchAllUserDrinks();
startManagingCursor(cursor);
String[] from = new String[] { OverLimitDbAdapter.KEY_USERNAME };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.user_row, cursor, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.profile:
Intent myIntent1 = new Intent(Overthelimit.this, Profile.class);
myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
startActivityForResult(myIntent1, 0);
return true;
case R.id.myusual:
Intent myIntent2 = new Intent(Overthelimit.this, MyUsual.class);
startActivityForResult(myIntent2, 0);
return true;
case R.id.trackme:
Intent myIntent3 = new Intent(Overthelimit.this, TrackMe.class);
startActivityForResult(myIntent3, 0);
return true;
case R.id.moreinfo:
Intent myIntent4 = new Intent(Overthelimit.this, MoreInfo.class);
startActivityForResult(myIntent4, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbHelper != null) {
dbHelper.close();
}
}
}
然后选择配置文件菜单选项将我带到配置文件,该配置文件崩溃尝试获取额外内容。 我已经使用吐司检查了数据,它就在那里。任何想法?
package uk.co.obdesign.android.overthelimit;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class Profile extends Activity{
private EditText mUserName;
private RadioGroup mGender;
private EditText mAge;
private EditText mHeight;
private EditText mWeight;
private Spinner mDrinkerType;
private Long mRowId;
private OverLimitDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
mDbHelper = new OverLimitDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.profile);
mUserName = (EditText) findViewById(R.id.userName);
mAge = (EditText) findViewById(R.id.age);
mHeight = (EditText) findViewById(R.id.height);
mWeight = (EditText) findViewById(R.id.weight);
mGender = (RadioGroup) findViewById(R.id.gender);
mDrinkerType = (Spinner) findViewById(R.id.drinkerType);
Button next = (Button) findViewById(R.id.NextUsualDrinks);
mRowId = (bundle == null) ? null :
(Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(OverLimitDbAdapter.KEY_ROWID)
: null;
}
populateFields();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(RESULT_OK);
//finish();
Intent myIntent = new Intent(view.getContext(), MyUsual.class);
startActivityForResult(myIntent, 0);
}
});
}
private void populateFields() {
if (mRowId != null) {
Cursor todo = mDbHelper.fetchUserDrinks(mRowId);
startManagingCursor(todo);
mUserName.setText(todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_USERNAME)));
String gender = todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_GENDER));
// Returns an integer which represents the selected radio button's ID
int selected = mGender.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
String bn = (String) b.getText();
if (bn.equalsIgnoreCase(gender)) {
if(gender == "male")
((RadioButton) findViewById(R.id.male)).setChecked(true);
else if (gender == "female")
((RadioButton) findViewById(R.id.female)).setChecked(true);
}
mAge.setText(todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_AGE)));
mHeight.setText(todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_HEIGHT)));
mWeight.setText(todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_WEIGHT)));
String drinkerType = todo.getString(todo
.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_TYPE));
for (int i = 0; i < mDrinkerType.getCount(); i++) {
String s = (String) mDrinkerType.getItemAtPosition(i);
Log.e(null, s + " " + drinkerType);
if (s.equalsIgnoreCase(drinkerType)) {
mDrinkerType.setSelection(i);
}
}
}
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(OverLimitDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
private void saveState() {
String username = mUserName.getText().toString();
// Returns an integer which represents the selected radio button's ID
int selected = mGender.getCheckedRadioButtonId();
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
// Now you can get the text or whatever you want from the "selected" radio button
String gender = (String) b.getText();
String age = mAge.getText().toString();
String height = mHeight.getText().toString();
String weight = mWeight.getText().toString();
String type = (String) mDrinkerType.getSelectedItem();
if (mRowId == null) {
long id = mDbHelper.createUserProfile(username, gender, age, height, weight, type);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateUserProfile(mRowId, username, gender, age, height, weight, type);
}
}
}
答案 0 :(得分:2)
您的问题很可能是您添加String
额外费用(cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID))
返回String
) - 但尝试获得Long
额外费用 - 这不会计算
以下内容应解决问题:
mRowId = extras != null ? Long.parseLong( extras.getString(OverLimitDbAdapter.KEY_ROWID) ) : null;
答案 1 :(得分:1)
在Overthelimit
课程中,您可以使用以下行设置值:
myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
这会将 String 值放入intent extras中。尝试获取与 Long 值相同的值将使应用程序崩溃,因为它不作为Long值存在。如果你转换从光标对象获得的值(或者从光标直接获得它),你的应用程序就不应该崩溃了。或者,您可以使用getString()
而非getLong()
从意图附加内容中获取价值。
答案 2 :(得分:0)
我认为它给出了类型转换错误,因为你将值存储为put,在try中输入类型转换时放置值时间或获取as字符串然后转换为long值但是对于类型转换可能是你得到类型转换错误在这里放入try catch块并查看logcat详细信息以检测错误
在此处输入从string到long或
的类型myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID,cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
或在此处获取字符串,然后从string键入cast to long
String s = extras.getString(OverLimitDbAdapter.KEY_ROWID);
mRowId = extras != null ? Long.parseLong(s): null;