2014-09-28 38 views
1

我抽屜導航存在一些問題。我嘗試了很多,並搜索了很多,但沒有找到解決方案。 我正在使用抽屜導航。你必須用碎片填充它的框架佈局。 這工作正常,所以例如: 我在抽屜導航中選擇了一些東西,它的框架佈局通過按鈕和文字視圖加載了片段(在我的應用程序中的startdisplay)。 現在我點擊其中一個片段按鈕,它應該在片段java類(startdisplay.java)中調用actionlistener,但是卻沒有找到這些動作偵聽器(android studio也告訴我這些從未使用過)。我發現應用程序正在查看抽屜導航類(我的應用程序中的mainmenuactivity)。抽屜導航,它的碎片按鈕沒有找到合適的動作偵聽器

我的問題是,如果有一種方式,我的應用程序直接在片段Java類,因爲我不能把我所有的代碼放在一個類(mainmenuactivity)。當然,這不會是我必須放在那裏的唯一片段。

這裏是代碼: 的Startdisplay片段的XML:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="de.touristenfahrerforum.MarcelMoiser.Fragments.Startdisplay"> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:id="@+id/startButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="114dp" 
     android:onClick="startButton" 
     android:text="Start" 
     android:textSize="50sp" /> 
.... 

的Java類Startdisplay片段:

public class Startdisplay extends Fragment 
{ 
.... 
private Activity activity; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.fragment_startdisplay, container, false); 

    loadTrackText = (TextView)view.findViewById(R.id.loadTrackText); 
    loadModeText = (TextView)view.findViewById(R.id.loadModeText); 
    loadBestlapText = (TextView)view.findViewById(R.id.loadBestlapText); 

    fileService = new Fileservice(); 

    activity = this.getActivity(); 

    return view; 
} 
.... 
/** 
* Actionlistener für den Start Button 
*/ 
public void startButton(View view) //!!!!!!!!!!!!!!This method should be called when the button is pressed <------------------------------------------------------------- 
{ 
    if(track != null) 
    { 
     if(((LocationManager) activity.getSystemService(activity.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER)) 
     {//GPS ist aktiviert 
      Intent intent = new Intent(activity, MainActivity.class); 

      intent.putExtra(V.TRACK_EXTRA, track); //Hier könnte man vll auch nur den String statt dem Objekt übergeben <------ 
      intent.putExtra(V.MODE_EXTRA, mode); 
      intent.putExtra(V.BESTLAP_EXTRA, bestlap); 

      startActivity(intent); 
     } 
     else{}//Meldung geben das das GPS nicht aktiviert ist 
    } 
    else{}//else es muss erst eine Strecke geladen werden 
} 
.... 

的MAINMENU XML(抽屜佈局):

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_menu" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111" /> 

</android.support.v4.widget.DrawerLayout> 

和Mainmenuactivity(應用程序搜索startBut的位置噸的ActionListener):

public class MainMenuActivity extends Activity 
{ 
    private String[] menuItems; 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 

    private CharSequence mTitle; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_menu); 

     //Standartwerte für die Einstellungen erzeugen 
     PreferenceManager.setDefaultValues(this, R.xml.preferences, false); 

     mTitle = getTitle(); 
     menuItems = new String[]{"Peter","Susi","Elephant"}; 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.main_menu); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     //Holt sich den Wert für die Umrechnung zwischen PX und DP 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     V.LOGICAL_DENSITY = metrics.density; 

     // Set the adapter for the list view 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, menuItems)); 
     // Set the list's click listener 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
    } 

    /* The click listner for ListView in the navigation drawer */ 
    private class DrawerItemClickListener implements ListView.OnItemClickListener 
    { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     { 
      selectItem(position); 
     } 
    } 

    /** Swaps fragments in the main content view */ 
    private void selectItem(int position) 
    { 
     // Create a new fragment and specify the planet to show based on position 


     // Insert the fragment by replacing any existing fragment 
     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frame, new Startdisplay()) 
     .commit(); 

     // Highlight the selected item, update the title, and close the drawer 
     mDrawerList.setItemChecked(position, true); 
     setTitle(menuItems[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

    @Override 
    public void setTitle(CharSequence title) 
    { 
     mTitle = title; 
     getActionBar().setTitle(mTitle); 
    } 
} 

我只是測試,所以的菜單項有沒有意義,它總是加載Startdisplay片段,無論我點擊。就像我已經說過的那樣,應用程序應該在Startdisplay.java中使用actionlistener,並且不應該在MainMenuActivity.java中搜索它。 在此先感謝

+0

如果你希望有人回答你的問題,請提供你的代碼。 – 2014-09-28 14:35:33

+0

好的,我添加了代碼。因爲它太多了,我認爲如果我能夠正確解釋問題會更容易。 – 2014-09-28 15:50:50

回答

1

您的問題是當您設置屬性,該方法在該視圖的背景下尋找。視圖的上下文當然是活動而不是片段,這就是爲什麼Android無法在那裏找到你的方法的原因。所以你有兩個選擇:

  1. 把方法放入活動並委託給片段。
  2. 改爲使用View.OnClickListener

總的來說,我個人並不喜歡使用,因爲它很容易打破。如果它不起作用,就像你的情況一樣,它只是默默地失敗。因此,我會用第二種方法。

呼叫像這樣的片段onCreateView()

view.findViewById(R.id.startButton).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //Do what you need 
    } 
}); 
+0

謝謝,優秀的答案。這正是我想要的,我知道我可以像你的選項1那樣做,但是這會在一個類中產生太多不必要的代碼。我嘗試了選項2,它工作。 – 2014-09-28 16:37:53