2

我有一个应用程序,当我单击设置按钮时,我需要移动到另一个包含选项卡的活动。我构建了所有代码,它在 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

我没有找到解决问题的解决方案。任何人都可以找到解决方案。

提前致谢...

4

3 回答 3

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()

于 2013-03-28T12:05:26.240 回答
0

由于您没有将任何数据从 Preference Activity 传递回第一个 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();

        }
    });
于 2013-03-28T14:39:16.960 回答
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  
             }
         } 

首选项.java

  pref_close.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) { 

      setResult(RESULT_OK);
      finish();
    }
});
于 2013-03-29T04:30:10.327 回答