2015-04-07 52 views
-2

我試圖使用預先在Android Studio中構建的導航欄,並找到了關於如何使用它的幾個指南,但其中大多數都是過時的或出於某種原因不要工作。關於newInstance()使用片段的錯誤

我使用一個現在看來「工作」,但我對newInstance有()
錯誤代碼得到一個錯誤:

錯誤:(58,59)錯誤:類FirstFragment方法的newInstance不能應用於給定類型; 要求:字符串,字符串發現 :沒有參數 原因:實際的和正式的參數列表長度

這裏不同的是我的代碼: `包welovecode.se.outfitted;

import android.app.Activity; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.content.Context; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v4.widget.DrawerLayout; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity 
    implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

/** 
* Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
*/ 
private NavigationDrawerFragment mNavigationDrawerFragment; 

/** 
* Used to store the last screen title. For use in {@link #restoreActionBar()}. 
*/ 
private CharSequence mTitle; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) 
      getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 

    // Set up the drawer. 
    mNavigationDrawerFragment.setUp(
      R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 
} 

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 

    FragmentManager fragmentManager = getSupportFragmentManager(); 

    if (position == 0) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, FirstFragment.newInstance()) 
       .commit(); 
    } else if (position == 1) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, SecondFragment.newInstance()) 
       .commit(); 
    } else if (position == 2) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, ThirdFragment.newInstance()) 
       .commit(); 
    } 

} 

public void onSectionAttached(int number) { 
    switch (number) { 
     case 1: 
      mTitle = getString(R.string.title_section1); 
      break; 
     case 2: 
      mTitle = getString(R.string.title_section2); 
      break; 
     case 3: 
      mTitle = getString(R.string.title_section3); 
      break; 
    } 
} 

public void restoreActionBar() { 
    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBar.setTitle(mTitle); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    if (!mNavigationDrawerFragment.isDrawerOpen()) { 
     // Only show items in the action bar relevant to this screen 
     // if the drawer is not showing. Otherwise, let the drawer 
     // decide what to show in the action bar. 
     getMenuInflater().inflate(R.menu.main, menu); 
     restoreActionBar(); 
     return true; 
    } 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MainActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 
} 

} 
` 

非常感謝你提前!

+0

你甚至知道方法的概念嗎? – Ryan

回答

1

該錯誤信息豐富。你有FirstFragment,SecondFragment等類,它們定義了帶參數的newInstance方法,在這裏是兩個字符串。然而,你正在調用那些沒有任何參數的方法。 所以解決方案應該是顯而易見的,既可以提供所需的參數,也可以修改那些不需要任何參數的類。很難告訴你你的代碼應該改變什麼,因爲1)我不知道你想要完成什麼,2)我沒有代碼片段。

+0

我只是想用導航抽屜在不同的片段之間切換。 –

+0

是的,但是,這並沒有什麼幫助。你自己寫FirstFragment嗎?還是來自其他地方?源代碼在哪裏?構造函數使用什麼參數?錯誤消息說兩個字符串。它們用於什麼? 您是否熟悉基本的Java,足以理解將參數傳遞給方法的概念? – JHH

0

在我看來,你想傳遞一個節號您的片段,如:

int sectNum = 100; 
fragmentManager.beginTransaction() 
    .replace(R.id.container, FirstFragment.newInstance(sectNum)) 

注:

  • newInstance方法調用只需要一個INT類型。
  • 您需要確定如何獲取部分編號。