2015-04-29 106 views
0

我已經開始在我的應用中使用側邊菜單創建片段inflated。問題不在於任何片段,其中之一就像使用android studio模板自動生成時一樣,似乎正在執行任何代碼。片段顯示在屏幕上時不運行片段代碼

這可能是我失去了一些明顯的東西,任何幫助將不勝感激。

這裏是我的MainActivity

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.*; 

import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 


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(); 
    fragmentManager.beginTransaction() 
      .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
      .commit(); 
}*/ 

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); 

    switch (position) { 
     case 1: 
    BlogFragment blogFragment = new BlogFragment(); 
    transaction.replace(R.id.action_bar, blogFragment); 
    } 

} 

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 = null; 
     rootView = inflater.inflate(R.layout.fragment_home, container, false); 
     switch(getArguments().getInt(ARG_SECTION_NUMBER)) { 
      case 1: 
       rootView = inflater.inflate(R.layout.fragment_home, container, false); 
       break; 
      case 2: 
       rootView = inflater.inflate(R.layout.fragment_blog, container, false); 
       break; 
      case 3: 
       rootView = inflater.inflate(R.layout.fragment_test, container, false); 
       break; 
      case 4: 
       //rootView = inflater.inflate(R.layout.fragment_info, container, false); 
       break; 
      default: 
       Log.e("TAG", "Unrecognized section: " + getArguments().getInt(ARG_SECTION_NUMBER)); 

     } 
     return rootView; 
    } 

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

} 

這裏是博客的片段,它試圖打開一個webview:提前

import android.app.Activity; 
import android.app.Fragment; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 


/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link BlogFragment.OnFragmentInteractionListener} interface 
* to handle interaction events. 
* Use the {@link BlogFragment#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class BlogFragment extends Fragment { 
// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

// TODO: Rename and change types of parameters 
private String mParam1; 
private String mParam2; 

private OnFragmentInteractionListener mListener; 

/** 
* Use this factory method to create a new instance of 
* this fragment using the provided parameters. 
* 
* @param param1 Parameter 1. 
* @param param2 Parameter 2. 
* @return A new instance of fragment blog. 
*/ 
// TODO: Rename and change types and number of parameters 
public static BlogFragment newInstance(String param1, String param2) { 
    BlogFragment fragment = new BlogFragment(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_PARAM1, param1); 
    args.putString(ARG_PARAM2, param2); 
    fragment.setArguments(args); 
    return fragment; 
} 

public BlogFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1); 
     mParam2 = getArguments().getString(ARG_PARAM2); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    WebView wv; 

    View view=inflater.inflate(R.layout.fragment_blog, null); 

    wv = (WebView) view.findViewById(R.id.webview); 
    WebSettings settings = wv.getSettings(); 
    wv.setWebChromeClient(new WebChromeClient() { 
    }); 
    final String mimeType = "text/html"; 
    final String encoding = "UTF-8"; 
    String html = getHTML(); 
    settings.setJavaScriptEnabled(true); 
    wv.loadDataWithBaseURL("", html, mimeType, encoding, ""); 

    return view; 

} 

// TODO: Rename method, update argument and hook method into UI event 
public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     mListener = (OnFragmentInteractionListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
       + " must implement OnFragmentInteractionListener"); 
    } 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 

public String getHTML() { 
    String html = "<iframe width=\"100%\" height=\"100%\" src=\"http://www.bbc.co.uk\" frameborder=\"0\" allowfullscreen></iframe>"; 
    return html; 
} 


/** 
* This interface must be implemented by activities that contain this 
* fragment to allow an interaction in this fragment to be communicated 
* to the activity and potentially other fragments contained in that 
* activity. 
* <p/> 
* See the Android Training lesson <a href= 
* "http://developer.android.com/training/basics/fragments/communicating.html" 
* >Communicating with Other Fragments</a> for more information. 
*/ 
public interface OnFragmentInteractionListener { 
    // TODO: Update argument type and name 
    public void onFragmentInteraction(Uri uri); 
} 

}

感謝。

回答

2

查明錯誤,你應該添加一個默認塊到您的switch語句:

switch(getArguments().getInt(ARG_SECTION_NUMBER)) { 
     case 1: 
      rootView = inflater.inflate(R.layout.fragment_home, container, false); 
      break; 
     case 2: 
      rootView = inflater.inflate(R.layout.fragment_blog, container, false); 
      break; 
     case 3: 
      rootView = inflater.inflate(R.layout.fragment_test, container, false); 
      break; 
     case 4: 
      //rootView = inflater.inflate(R.layout.fragment_info, container, false); 
      break; 
     default: 
      Log.e("TAG", "Unrecognized section: " + getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 

在我看來,當onCreateView被稱爲參數沒有設置。如果未設置,則getInt()返回0,這不屬於switch語句中的任何情況。

編輯:

仔細檢查後,相信你應該罰款與一個簡單的抽屜項目選擇監聽器:

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction transation = fragmentManager.beginTransaction(); 
    switch (position) { 
     case 1: 
      HomeFragment homeFragment = new HomeFragment(); 
      homeFragment.setArguments(someArgs); 
      transation.replace(R.id.container, homeFragment); 
      break; 
     case 2: 
      BlogFragment blogFragment = new BlogFragment(); 
      blogFragment.setArguments(someArgs); 
      transation.replace(R.id.container, blogFragment); 
      break; 
     case 3: 
      TestFragment testFragment = new TestFragment(); 
      testFragment.setArguments(someArgs); 
      transation.replace(R.id.container, testFragment); 
      break; 
     case 4: 
      //rootView = inflater.inflate(R.layout.fragment_info, container, false); 
      break; 
     default: 
      return; 
    } 
    transation.commit(); 
} 
+0

謝謝你的迴應,我現在試試這個。我會在哪裏看到來自日誌命令的反饋? – Lister

+1

@Lister在您的IDE的logcat中。 – Simas

+0

我試過在兩個android設備上運行這個,並嘗試在onCreateView和日誌行的開頭使用斷點,但我無法看到日誌中的任何內容,並且斷點沒有激活。 – Lister