[英]Android:startActivityForResult( ) is not calling
I have one application in that when I click on settings button, I need to move to another activity which contains tabs. 我有一个应用程序,当我单击“设置”按钮时,我需要转到另一个包含选项卡的活动。 I build up all the code and it is working well for Acer tab. 我建立了所有代码,并且对于Acer选项卡运行良好。 When I run the same application in Samsung Galaxy(4.1.1) It is showing "Unfortunately,app has stopped" error. 当我在Samsung Galaxy(4.1.1)中运行相同的应用程序时,显示“不幸的是,应用程序已停止”错误。 Check my code below: 检查下面的代码:
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getContext(), Preferences.class);
startActivityForResult(prefIntent, 0);
}
});
//Preferences.java //Preferences.java
setContentView(R.layout.preferences);
private Button pref_close;
pref_close = (Button) findViewById(R.id.close_prefs);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("wifi").setIndicator("Wifi Settings",null).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent(this, AppSettings.class);
spec = tabHost.newTabSpec("settings").setIndicator(" Clock Settings",null).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//finish();
setResult(RESULT_OK);
}
});
The issue is here when I comment the startActivityForResult() then my app is not force closing and preferences.java file is not calling. 问题出在这里,当我评论startActivityForResult()时,我的应用程序没有被强制关闭,preferences.java文件未调用。 In my logcat show any errors, but showing as 在我的logcat中显示任何错误,但显示为
03-28 11:30:37.734: E/AndroidRuntime(4668): java.lang.RuntimeException: Unable to resume activity {com.vision.clock/com.vision.clock.activity.Preferences}: java.lang.RuntimeException: Unable to resume activity {com.android.settings/com.android.settings.wifi.WifiPickerActivity}: java.lang.SecurityException: Given caller package com.android.settings is not running in process ProcessRecord
I'm not getting solution to find out the issue.. Can anybody find out the solution. 我没有找到解决问题的方法。任何人都可以找到解决方法。
Thanks in advance... 提前致谢...
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getApplicationContext(), Preferences.class);
startActivityForResult(prefIntent, 0);
}
});
Change v.getContext() into v.getApplicationContext() 将v.getContext()更改为v.getApplicationContext()
Since you are not passing any data back from Preference Activity to the first activity, you do not need to implement startActivityForResult in the first place. 由于您没有将任何数据从“首选项活动”传递回第一个活动,因此您无需首先实现startActivityForResult。
In First Activity: 在第一个活动中:
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getContext(), Preferences.class);
startActivityForResult(prefIntent);
}
});
In Preferences activity: 在“首选项”活动中:
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
first Activity 第一次活动
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getContext(), Preferences.class);
startActivityForResult(prefIntent, 0);
}
});
////this one add to your first activity after oncreate close
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
// your code
}
}
Preferences.java Preferences.java
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.