在android中控制后退“按钮”
Posted
技术标签:
【中文标题】在android中控制后退“按钮”【英文标题】:Control the back "button" in android 【发布时间】:2010-11-18 21:39:52 【问题描述】:我想从子活动中获取文本数据值。一切都很好。但是当打开子活动时,我只需点击手机上的“返回按钮”,就会抛出异常错误。我在 android NotePad life-cycle control toturial 上找到了,但很难理解。有人能帮我吗?这是我的代码:
public class SBooks extends ListActivity
private String title_raw;
private SBooksDbAdapter mDbHelper;
private static final int ACTIVITY_SEARCH = 0;
private static final int SEARCH_ID = Menu.FIRST;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.sbooks_list);
mDbHelper = new SBooksDbAdapter(this);
mDbHelper.open();
// Create Menu Option
@Override
public boolean onCreateOptionsMenu(Menu menu)
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, SEARCH_ID, 0, R.string.menu_search);
return result;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch(item.getItemId())
case SEARCH_ID:
Intent intent = new Intent(this, SBooksSearch.class);
startActivityForResult(intent, ACTIVITY_SEARCH);
return super.onOptionsItemSelected(item);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode)
case ACTIVITY_SEARCH:
Bundle bundle = intent.getExtras();
title_raw = bundle.getString(SBooksDbAdapter.KEY_TITLE_RAW);
if(title_raw!=null)
Cursor cursor = mDbHelper.searchData(title_raw);
String[] from = new String[] SBooksDbAdapter.KEY_ROWID,
SBooksDbAdapter.KEY_TITLE, SBooksDbAdapter.KEY_LYRICS ;
int[] to = new int[] R.id.id, R.id.title, R.id.lyrics ;
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.sbooks_row, cursor, from, to );
setListAdapter(adapter);
这是我的子活动:
public class SBooksSearch extends Activity
private EditText mTextSearch;
private Button searchButton;
private SBooksDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sbooks_search);
mTextSearch = (EditText)findViewById(R.id.text_search);
searchButton = (Button)findViewById(R.id.btn_search);
searchButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
Intent intent = new Intent();
intent.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());
setResult(RESULT_OK, intent);
finish();
);
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
@Override
protected void onPause()
super.onPause();
checkState();
@Override
protected void onResume()
super.onResume();
private void checkState()
这是Logcat:
08-12 18:44:39.211: WARN/InputManagerService(581): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43719140 (uid=10004 pid=623)
08-12 18:44:39.510: INFO/ActivityManager(581): Displayed activity com.original.sbooks/.SBooks: 4934 ms
08-12 18:44:40.821: WARN/KeyCharacterMap(730): No keyboard for id 0
08-12 18:44:40.821: WARN/KeyCharacterMap(730): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-12 18:44:41.412: INFO/ARMAssembler(581): generated scanline__00000177:03515104_00001A01_00000000 [ 64 ipp] (89 ins) at [0x297608:0x29776c] in 952924 ns
08-12 18:44:41.520: INFO/ARMAssembler(581): generated scanline__00000177:03515104_00000A01_00000000 [ 46 ipp] (70 ins) at [0x297798:0x2978b0] in 517061 ns
08-12 18:44:42.171: INFO/ActivityManager(581): Starting activity: Intent comp=com.original.sbooks/com.original.sbooks.SBooksSearch
08-12 18:44:52.196: WARN/ActivityManager(581): Launch timeout has expired, giving up wake lock!
08-12 18:44:52.279: WARN/ActivityManager(581): Activity idle timeout for HistoryRecord436bfc80 com.original.sbooks/com.original.sbooks.SBooksSearch
08-12 18:44:57.350: DEBUG/dalvikvm(620): GC freed 4103 objects / 231808 bytes in 75ms
08-12 18:45:05.130: DEBUG/dalvikvm(649): GC freed 2604 objects / 150112 bytes in 88ms
08-12 18:45:10.120: DEBUG/dalvikvm(623): GC freed 2750 objects / 149592 bytes in 71ms
08-12 18:45:11.920: INFO/ActivityManager(581): Displayed activity com.original.sbooks/.SBooksSearch: 29748 ms
08-12 18:45:17.263: WARN/ActivityManager(581): Activity pause timeout for HistoryRecord436bfc80 com.original.sbooks/com.original.sbooks.SBooksSearch
08-12 18:45:26.762: WARN/ActivityManager(581): Launch timeout has expired, giving up wake lock!
08-12 18:45:27.272: WARN/ActivityManager(581): Activity idle timeout for HistoryRecord435a7760 com.original.sbooks/com.original.sbooks.SBooks
08-12 18:46:04.905: DEBUG/AndroidRuntime(730): Shutting down VM
08-12 18:46:04.905: WARN/dalvikvm(730): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
08-12 18:46:04.905: ERROR/AndroidRuntime(730): Uncaught handler: thread main exiting due to uncaught exception
08-12 18:46:04.991: ERROR/AndroidRuntime(730): java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=0, result=0, data=null to activity com.original.sbooks/com.original.sbooks.SBooks: java.lang.NullPointerException
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.deliverResults(ActivityThread.java:3005)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.access$2300(ActivityThread.java:112)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.os.Looper.loop(Looper.java:123)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at dalvik.system.NativeStart.main(Native Method)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): Caused by: java.lang.NullPointerException
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at com.original.sbooks.SBooks.onActivityResult(SBooks.java:111)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
08-12 18:46:04.991: ERROR/AndroidRuntime(730): at android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
【问题讨论】:
如果您希望人们在遇到“异常错误”时帮助您,如果您发布与异常相关的 Java 堆栈跟踪信息,那将非常有帮助。这可以通过 adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图找到。 我发布logcat,这就是你想要的?抱歉,我没有在 android 中调试的经验。感谢您的帮助! 【参考方案1】:我认为 Nate 对 Intent.getExtras() 处的 NPE 是正确的。
您应该在执行任何操作之前检查结果代码的值。在获得该值之前,您可能还需要检查以确保 intent.hasExtra(SBooksDbAdapter.KEY_TITLE_RAW)。所以,是这样的:
switch(requestCode)
case ACTIVITY_SEARCH:
if (resultCode == RESULT_OK && intent.hasExtra(SBooksDbAdapter.KEY_TITLE_RAW))
title_raw = intent.getStringExtra(SBooksDbAdapter.KEY_TITLE_RAW);
... do all that other stuff ...
【讨论】:
【参考方案2】:尝试在主要活动的 onActivityResult(...) 方法中加入一些日志记录。子活动的“后退”按钮可能会为您提供 RESULT_CANCELED 的结果代码,在这种情况下,意图可以为空。所以你会在
获得NPE捆绑包 = intent.getExtras();
【讨论】:
“记录”是什么意思?我不知道该怎么做。 简单的方法是 System.out.println ("++++++++++++ " + resultCode);然后在控制台中观看“adb -e logcat”并寻找优点。 推荐的(同样简单的)方法是使用 Log 类:Log.i("your-log-tag", "your message");
。您可以在 LogCat 中查看您的日志记录,还可以为“your-log-tag”设置一个过滤器以仅查看您自己的消息。以上是关于在android中控制后退“按钮”的主要内容,如果未能解决你的问题,请参考以下文章
android中的后退按钮打破nativescript-vue访问控制登录注销