我有一个应用程序,当我点击设置按钮时,我需要移动到另一个包含标签的活动。我构建了所有代码,它适用于Acer选项卡。当我在三星Galaxy(4.1.1)中运行相同的应用程序时显示“不幸的是,应用已停止”错误。检查下面的代码:
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getContext(), Preferences.class);
startActivityForResult(prefIntent, 0);
}
});
// 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);
}
});
当我发表评论startActivityForResult()然后我的应用程序没有强制关闭而且preferences.java文件没有调用时,问题出在这里。在我的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
我没有找到问题的解决方案..任何人都可以找到解决方案。
提前致谢...
答案 0 :(得分:0)
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getApplicationContext(), Preferences.class);
startActivityForResult(prefIntent, 0);
}
});
将v.getContext()更改为v.getApplicationContext()
答案 1 :(得分:0)
由于您没有将任何数据从Preference Activity传回第一个活动,因此您不需要首先实现startActivityForResult。
在第一项活动中:
settingsBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent prefIntent = new Intent(v.getContext(), Preferences.class);
startActivityForResult(prefIntent);
}
});
在偏好设置活动中:
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
答案 2 :(得分:0)
第一个活动
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
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});