简体   繁体   中英

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. When I run the same application in Samsung Galaxy(4.1.1) It is showing "Unfortunately,app has stopped" error. 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

   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. In my logcat show any errors, but showing as

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

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.

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

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

      setResult(RESULT_OK);
      finish();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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