繁体   English   中英

Android:startActivityForResult()未调用

[英]Android:startActivityForResult( ) is not calling

我有一个应用程序,当我单击“设置”按钮时,我需要转到另一个包含选项卡的活动。 我建立了所有代码,并且对于Acer选项卡运行良好。 当我在Samsung 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

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

提前致谢...

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

由于您没有将任何数据从“首选项活动”传递回第一个活动,因此您无需首先实现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();

        }
    });

第一次活动

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM