簡體   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