2017-06-19 78 views
0

我在Java編程中還是比較新,我想用幾個按鈕打開一個片段不同的活動。不過,我總是得到一個錯誤(...在已定義...)在以下位置:在帶有幾個按鈕的片段中開始活動

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) 
在其它變型中,沒有工作

也有人甚至更多的錯誤。

這裏是我的片段全碼:

public DashboardFragment() { 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 

    ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(), Stundenplan.class); 
      startActivity(intent); 


     } 
    }); 

    return rootView; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn); 
    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(), Vertretungsplan.class); 
      startActivity(intent); 


     } 
    }); 

    return rootView; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.essenbtn); 
    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(), Essen.class); 
      startActivity(intent); 


     } 
    }); 

    return rootView; 
} 
} 
+0

你會得到什麼錯誤? – moondaisy

回答

3

你真正想要的是這樣的:因爲你沒有使用ImageButton類的任何具體方法

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 

    ImageButton button = (ImageButton) rootView.findViewById(R.id.stundenplanbtn); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(getActivity(), Stundenplan.class); 
      startActivity(intent); 


     } 
    }); 

    ImageButton button2 = (ImageButton) rootView.findViewById(R.id.vertretungsbtn); 
     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getActivity(), Vertretungsplan.class); 
       startActivity(intent); 


      } 
     }); 

    return rootView; 

} 

而且,你不是真的所有的ImageButton鑄造Android特定背景等,並因爲它extendsView類,你可以簡單地使用:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 


    rootView.findViewById(R.id.stundenplanbtn).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getActivity(), Stundenplan.class); 
       startActivity(intent); 


      } 
     }); 

    rootView.findViewById(R.id.vertretungsbtn).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Intent intent = new Intent(getActivity(), Vertretungsplan.class); 
        startActivity(intent); 


       } 
      }); 

return rootView; 

} 

注意唯一的事情在這裏並不創建button變量/引用,所以這只是一個小改進。但是,如果這讓你感到困惑,那麼就忽略並使用第一個。

0

你有同樣的方法,在同一個類中定義倍,但只能有一個方法具有相同的名稱,也帶有@Override標記的方法需要重寫實際的超類方法,因此您不能只調用方法onCreateView2()並保留標記。

只需刪除底部的兩個方法。