Fragmentを利用し、画面遷移を試みているところです。
MainActiityの()にエラーが出てしまい
改善方法がわからない状況です。
エラー内容は
エラー: クラス MainFragmentのメソッド newInstanceは指定された型に適用できません。
期待値: String,String
検出値: 引数がありません
理由: 実引数リストと仮引数リストの長さが異なります
と出ています。
実取引リストと仮引数リストの個数が違うというのは
多々サイトを参考にしたので理解いたしましたが
具体的にどのようにしたらいいのかがわかりません。
わかりやすいような参考サイトのご提供でも構いません、
アドバイスお願いいたしますmm
java
1MainActivity 2package jp〜〜〜〜〜〜〜〜〜 3 4import android.os.Bundle; 5import android.support.annotation.NonNull; 6import android.support.design.widget.BottomNavigationView; 7import android.support.v4.app.FragmentTransaction; 8import android.support.v7.app.AppCompatActivity; 9import android.view.MenuItem; 10import android.widget.TextView; 11 12public class MainActivity extends AppCompatActivity { 13 14 private TextView mTextMessage; 15 16 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 17 = new BottomNavigationView.OnNavigationItemSelectedListener() { 18 19 @Override 20 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 21 switch (item.getItemId()) { 22 case R.id.navigation_product: 23 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 24 transaction.replace(R.id.container,MainFragment.newInstance()); 25 transaction.commit(); 26 return true; 27 case R.id.navigation_cart: 28 return true; 29 case R.id.navigation_delivery_status: 30 return true; 31 case R.id.navigation_mypage: 32 return true; 33 } 34 return false; 35 } 36 }; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 43 mTextMessage = (TextView) findViewById(R.id.message); 44 BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 45 navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 46 } 47 48}
java
1MainFragment 2 3package jp〜〜〜〜〜〜〜〜〜 4 5import android.content.Context; 6import android.net.Uri; 7import android.os.Bundle; 8import android.support.v4.app.Fragment; 9import android.view.LayoutInflater; 10import android.view.View; 11import android.view.ViewGroup; 12 13 14/** 15 * A simple {@link Fragment} subclass. 16 * Activities that contain this fragment must implement the 17 * {@link MainFragment.OnFragmentInteractionListener} interface 18 * to handle interaction events. 19 * Use the {@link MainFragment#newInstance} factory method to 20 * create an instance of this fragment. 21 */ 22public class MainFragment extends Fragment { 23 // TODO: Rename parameter arguments, choose names that match 24 // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 25 private static final String ARG_PARAM1 = "param1"; 26 private static final String ARG_PARAM2 = "param2"; 27 28 // TODO: Rename and change types of parameters 29 private String mParam1; 30 private String mParam2; 31 32 private OnFragmentInteractionListener mListener; 33 34 public MainFragment() { 35 // Required empty public constructor 36 } 37 38 /** 39 * Use this factory method to create a new instance of 40 * this fragment using the provided parameters. 41 * 42 * @param param1 Parameter 1. 43 * @param param2 Parameter 2. 44 * @return A new instance of fragment MainFragment. 45 */ 46 // TODO: Rename and change types and number of parameters 47 public static MainFragment newInstance(String param1, String param2) { 48 MainFragment fragment = new MainFragment(); 49 Bundle args = new Bundle(); 50 args.putString(ARG_PARAM1, param1); 51 args.putString(ARG_PARAM2, param2); 52 fragment.setArguments(args); 53 return fragment; 54 } 55 56 @Override 57 public void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 if (getArguments() != null) { 60 mParam1 = getArguments().getString(ARG_PARAM1); 61 mParam2 = getArguments().getString(ARG_PARAM2); 62 } 63 } 64 65 @Override 66 public View onCreateView(LayoutInflater inflater, ViewGroup container, 67 Bundle savedInstanceState) { 68 // Inflate the layout for this fragment 69 return inflater.inflate(R.layout.fragment_main, container, false); 70 } 71 72 // TODO: Rename method, update argument and hook method into UI event 73 public void onButtonPressed(Uri uri) { 74 if (mListener != null) { 75 mListener.onFragmentInteraction(uri); 76 } 77 } 78 79 @Override 80 public void onAttach(Context context) { 81 super.onAttach(context); 82 if (context instanceof OnFragmentInteractionListener) { 83 mListener = (OnFragmentInteractionListener) context; 84 } else { 85 throw new RuntimeException(context.toString() 86 + " must implement OnFragmentInteractionListener"); 87 } 88 } 89 90 @Override 91 public void onDetach() { 92 super.onDetach(); 93 mListener = null; 94 } 95 96 /** 97 * This interface must be implemented by activities that contain this 98 * fragment to allow an interaction in this fragment to be communicated 99 * to the activity and potentially other fragments contained in that 100 * activity. 101 * <p> 102 * See the Android Training lesson <a href= 103 * "http://developer.android.com/training/basics/fragments/communicating.html" 104 * >Communicating with Other Fragments</a> for more information. 105 */ 106 public interface OnFragmentInteractionListener { 107 // TODO: Update argument type and name 108 void onFragmentInteraction(Uri uri); 109 } 110} 111
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/12 05:30