2015-03-31 65 views
1

我有錯誤在片段:在類型FragmentTransaction的方法Add(INT,片段)是不適用的參數(INT,WeatherFragment)

「的方法,在類型取代(INT,片段)分段交易是 不適用於參數(int,Fragment)「

我該怎麼辦?我的片段不出現。

package com.example.weather; 

    import android.support.v7.app.ActionBarActivity; 

    import android.support.v4.app.Fragment; 

    import android.text.InputType; 
    import android.app.AlertDialog; 

    import android.content.DialogInterface; 

    import android.os.Bundle; 

    import android.view.LayoutInflater; 

    import android.view.Menu; 

    import android.view.MenuItem; 

    import android.view.View; 

    import android.view.ViewGroup; 

    import android.widget.EditText; 

    import android.os.Build; 

    public class WeatherActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_weather); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new WeatherFragment()) 
        .commit(); 
     } 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if(item.getItemId() == R.id.change_city){ 
      showInputDialog(); 
     } 
     return false; 
    } 

    private void showInputDialog(){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Change city"); 
     final EditText input = new EditText(this); 
     input.setInputType(InputType.TYPE_CLASS_TEXT); 
     builder.setView(input); 
     builder.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       changeCity(input.getText().toString()); 
      } 
     }); 
     builder.show(); 
    } 

    public void changeCity(String city){ 
     WeatherFragment wf = (WeatherFragment) getFragmentManager() 
       .findFragmentById(R.id.container); 
     wf.changeCity(city); 
     new CityPreference(this).setCity(city); 
    } 
} 
+0

你有沒有定義你自己的稱爲Fragment的類型,它不擴展android.app.Fragment? – 2015-03-31 07:22:30

+1

標題中的錯誤與相關錯誤不符。請決定您需要幫助的錯誤。 – 2015-03-31 07:23:26

+0

對不起,說不清楚說明 – 2015-03-31 07:26:58

回答

-1

無論WeatherFragment是什麼,它不是Fragment類型,也不存在可以處理WeatherFragment的構造函數。 也許你可以將它投射到片段。不知道它是什麼,就說不出來。

清晨的錯誤。答案的第一部分是);他正在使用他自己的班級WeatherFragment。如果不能看到這一點,我認爲它不是一個(適當的)片段。

+0

你絕對不能把一些不是片段的子類型爲片段。 – 2015-03-31 07:28:18

+0

所以我用一塊Treeline代替它 – 2015-03-31 07:50:25

+0

沒有人幫我出局 請 – 2015-03-31 07:57:38

1

片段的導入應該改變。 這是錯誤的: -

import android.support.v4.app.Fragment;

正確導入: -

import android.app.Fragment;

+0

你怎麼知道他沒有使用支持片段? – Treeline 2015-03-31 07:42:17

+0

此前,我花了兩天的時間在初學者水平解決同樣的問題。所以我只是猜測它。可能有幫助 – 2015-03-31 07:44:10

+0

啊好吧。答案只是使它看起來像一個事實,但我不能確定你是如何知道這是問題:) – Treeline 2015-03-31 07:48:37

0

我有同樣的問題,我解決它,當我意識到:

WebViewFragment是子類android.app.Fragment,所以你不能使用android.support.v4.app.FragmentManager添加它。您必須使用android.app.FragmentManager

,所以我下面的步驟,以便能夠使用WebViewFragment

  1. 我改變我的片段的超類的android.app.Fragment代替android.support.v4.app.Fragment
  2. 我改變了FragmentManagerandroid.app.FragmentManager,而不是android.support.v4.app.FragmentManager
  3. 我用getFragmentManager()代替getSupportFragmentManager

我希望這會有所幫助。

相關問題