- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是新手,我试图让我的 ListView
在我的 SQLite 数据库更新后刷新。修改我的 onResume()
方法后,我没有收到编译错误。我正在使用 SimpleCursorAdapter
重新查询,但它不起作用。收到的错误来自 logcat,如下所示。请指教……示例最有帮助。
日志:
02-19 21:31:49.933: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to resume activity {com.loginplus.home/com.loginplus.home.LoginList}: java.lang.NullPointerException
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.os.Looper.loop(Looper.java:137)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-19 21:31:49.933: E/AndroidRuntime(714): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 21:31:49.933: E/AndroidRuntime(714): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 21:31:49.933: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-19 21:31:49.933: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-19 21:31:49.933: E/AndroidRuntime(714): at dalvik.system.NativeStart.main(Native Method)
02-19 21:31:49.933: E/AndroidRuntime(714): Caused by: java.lang.NullPointerException
02-19 21:31:49.933: E/AndroidRuntime(714): at com.loginplus.home.LoginList.onResume(LoginList.java:101)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.Activity.performResume(Activity.java:4539)
02-19 21:31:49.933: E/AndroidRuntime(714): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
Activity :
public class LoginList extends Activity implements OnClickListener, OnItemClickListener {
private ListView loginList;
private Button webLogin;
private ListAdapter loginListAdapter;
private ArrayList<LoginDetails> loginArrayList;
List<String> arrayList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
arrayList = populateList();
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
setContentView(R.layout.login_listview);
loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);
}
@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
public List<String> populateList (){
List<String> webNameList = new ArrayList<String>();
dataStore openHelperClass = new dataStore (this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
startManagingCursor(cursor);
while (cursor.moveToNext()){
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
LoginDetails lpDetails = new LoginDetails();
lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);
loginArrayList.add(lpDetails);
webNameList.add(sName);
}
sqliteDatabase.close();
return webNameList;
}
@Override
protected void onResume() {
super.onResume();
loginArrayList.clear();
arrayList.clear();
arrayList = populateList();
dataStore refreshHelper = new dataStore (this);
SQLiteDatabase sqliteDatabase = refreshHelper.getWritableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
String[]columns = new String[] { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES };
int[] to = new int[]{R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};
SimpleCursorAdapter loginListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);
loginListAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();
Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
LoginDetails clickedObject = loginArrayList.get(arg2);
Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",clickedObject.getsName());
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
loginBundle.putString("clickedUserName",clickedObject.getuName());
loginBundle.putString("clickedPassWord",clickedObject.getpWord());
loginBundle.putString("clickedNotes",clickedObject.getlNotes());
updateDeleteLoginInfo.putExtras(loginBundle);
startActivityForResult(updateDeleteLoginInfo, 0);
}
}
RennoDiniro 编辑结果:
日志:
02-21 23:40:18.419: E/AndroidRuntime(705): FATAL EXCEPTION: main
02-21 23:40:18.419: E/AndroidRuntime(705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loginplus.home/com.loginplus.home.LoginList}: java.lang.NullPointerException
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.os.Looper.loop(Looper.java:137)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-21 23:40:18.419: E/AndroidRuntime(705): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 23:40:18.419: E/AndroidRuntime(705): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 23:40:18.419: E/AndroidRuntime(705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 23:40:18.419: E/AndroidRuntime(705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 23:40:18.419: E/AndroidRuntime(705): at dalvik.system.NativeStart.main(Native Method)
02-21 23:40:18.419: E/AndroidRuntime(705): Caused by: java.lang.NullPointerException
02-21 23:40:18.419: E/AndroidRuntime(705): at com.loginplus.home.LoginList.populateList(LoginList.java:88)
02-21 23:40:18.419: E/AndroidRuntime(705): at com.loginplus.home.LoginList.onCreate(LoginList.java:37)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.Activity.performCreate(Activity.java:4465)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-21 23:40:18.419: E/AndroidRuntime(705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
Activity 类:
public class LoginList extends Activity implements OnClickListener, OnItemClickListener {
private ListView loginList;
private Button webLogin;
private ListAdapter loginListAdapter;
private ArrayList<LoginDetails> loginArrayList;
List<String> arrayList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
arrayList = populateList();
setContentView(R.layout.login_listview);
loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);
}
@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
public List<String> populateList (){
List<String> webNameList = new ArrayList<String>();
dataStore openHelperClass = new dataStore (this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
startManagingCursor(cursor);
while (cursor.moveToNext()){
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
LoginDetails lpDetails = new LoginDetails();
lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);
loginArrayList.add(lpDetails);
webNameList.add(sName);
}
sqliteDatabase.close();
return webNameList;
}
@Override
protected void onResume() {
super.onResume();
try{
loginArrayList = new ArrayList<LoginDetails>();
arrayList = new ArrayList<String>();
loginArrayList.clear();
arrayList.clear();
arrayList = populateList();
dataStore refreshHelper = new dataStore (this);
SQLiteDatabase sqliteDatabase = refreshHelper.getWritableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
String[]columns = new String[] { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES };
int[] to = new int[]{R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};
SimpleCursorAdapter loginListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);
loginListAdapter.notifyDataSetChanged();
}catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();
Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
LoginDetails clickedObject = loginArrayList.get(arg2);
Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",clickedObject.getsName());
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
loginBundle.putString("clickedUserName",clickedObject.getuName());
loginBundle.putString("clickedPassWord",clickedObject.getpWord());
loginBundle.putString("clickedNotes",clickedObject.getlNotes());
updateDeleteLoginInfo.putExtras(loginBundle);
startActivityForResult(updateDeleteLoginInfo, 0);
}
}
最佳答案
使用指向所需数据和布局信息的光标创建适配器。
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);
在您使用 R.layout.simple_list_item_1
columns
将为 null
或 您从游标获取的特定列数据
to
将是 android.R.id.text1
例如
Cursor cursor = getContentResolver().query(People.CONTENT_URI, new String[]{People._ID, People.NAME, People.NUMBER}, null, null, null);
startManagingCursor(cursor);
// THE DESIRED COLUMNS TO BE BOUND
String[] columns = new String[] { People.NAME, People.NUMBER };
// THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
int[] to = new int[] { R.id.name_entry, R.id.number_entry };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.list_example_entry, cursor, columns, to);
关于android - 无法恢复 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839734/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!