按下actionBar图标我应该转到之前的片段,但它不会。我的意思是当我按下操作栏图标时没有任何事情发生。
import java.awt.*;
import java.util.LinkedList;
import java.util.List;
public class Project1GUI {
static JFrame Frame;
static TextArea unsorted_words, sorted_words, linked_words;
public Project1GUI(String title){
//All this does is make an empty GUI FRAME.
Frame=new JFrame();//i made a new variable from the JFrame class
Frame.setSize(400,400);//Used the Variable from JFrame and used some of it functions. This function sets the hieght and width of the Frame
Frame.setLocation(200,200);//This sets where the Empty Frame should be
Frame.setTitle(title);//This puts a title up top of the Frame
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//places an x box that closes when clicked on
Frame.setVisible(true);//This activates the JFram when is set true.
Frame.setLayout(new GridLayout(1,2));//This sets the layout of the Frame and since i want a Grid i used a GirdLayout
//Functions and placed it inside the setlayout functions. to get 2 grids i places 1 meaning 1 row and 2 for 2 cols
unsorted_words=new TextArea(); //From the TextArea class i made three variables
sorted_words= new TextArea();
linked_words= new TextArea();
Container panel=new Container();
panel=Frame.getContentPane();
panel.add(unsorted_words);
panel.add(sorted_words);
panel.add(linked_words);
}
public void add_unsorted(String words){
unsorted_words.append(words+"\n");//add words to GUI
}
public void add_sorted(String words){
sorted_words.append(words+"\n");
}
public void add_linked(List<String> linked_words2){
linked_words.append(linked_words2+"\n");
}
}
所以它不会在switch语句中触发public class SecFrag extends Fragment {
// constructor and onCreateView goes here
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar=((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
recyclerView= (RecyclerView) getActivity().findViewById(R.id.recyclerViewInSecFragment);
RecyclerAdapter adapter=new RecyclerAdapter(MainActivity.musicList,null,null);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("SEC",item.getItemId()+"");
switch (item.getItemId())
{
case android.R.id.home:
{
Log.e("SECFRAG","CLICKED");
break;
}
default:break;
}
return super.onOptionsItemSelected(item);
}
}
。所以请说明为什么这不起作用。
答案 0 :(得分:1)
您需要通过调用setHasOptionsMenu(true)
来指示Android堆栈,您的片段将处理选项菜单而不是持有者活动,并且文档推荐将其放入您的片段中onCreate()
回调。这将使您的日志消息被适当调用。