我正在开发一个应用程序,每个选项卡有3个选项卡和片段。从选项卡中选择列表项时,新片段将替换同一选项卡中的当前片段。我在这个新片段中使用了一个onclicklistener
按钮。但是,当实现onClickMethod时,应用程序甚至会在加载选项卡之前崩溃。
public class ChatWindow extends Fragment {
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.chat_window, container, false);
}
public void onStart()
{
Button btn=(Button)getActivity().findViewById(R.id.btnSend);
Log.v("HI","Button Created !!");
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//adapter=new ArrayAdapter<String> (getActivity(),android.R.layout.simple_list_item_1,listItems);
//setListAdapter(adapter);
Log.v("HI","Button !!");
Toast toast = Toast.makeText(getActivity().getBaseContext(), "Hello",10000);
toast.show();
}
};
btn.setOnClickListener(listener);
}
}
这是logcat ::
01-09 18:02:22.126: D/gralloc_goldfish(623): Emulator without GPU emulation detected.
01-09 18:02:22.156: W/TextLayoutCache(623): computeValuesWithHarfbuzz -- need to force to single run
01-09 18:02:36.805: V/AB(623): Main Tab Activity
01-09 18:02:36.855: V/HI(623): Button Created !!
01-09 18:02:36.855: D/AndroidRuntime(623): Shutting down VM
01-09 18:02:36.865: W/dalvikvm(623): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-09 18:02:36.885: E/AndroidRuntime(623): FATAL EXCEPTION: main
01-09 18:02:36.885: E/AndroidRuntime(623): android.app.SuperNotCalledException: Fragment ChatWindow{41099e70 #1 id=0x7f040011} did not call through to super.onStart()
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:981)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1700)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.Activity.performStart(Activity.java:4481)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137)
01-09 18:02:36.885: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-09 18:02:36.885: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:02:36.885: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:02:36.885: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 18:02:36.885: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 18:02:36.885: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
在评论onclicklistener代码时,应用程序可以正常运行。
答案 0 :(得分:2)
替换
Button btn=(Button)getActivity().findViewById(R.id.btnSend);
带
Button btn=(Button)getView().findViewById(R.id.btnSend);
甚至在onClick()
内,它不需要getBaseContext()
。简单getActivity()
绰绰有余。
Toast toast = Toast.makeText(getActivity(), "Hello",10000);
答案 1 :(得分:0)
更改按钮的代码,如下所示..
Button btn=(Button)findViewById(R.id.btnSend);
Log.v("HI","Button Created !!");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//adapter=new ArrayAdapter<String> (getActivity(),android.R.layout.simple_list_item_1,listItems);
//setListAdapter(adapter);
Log.v("HI","Button !!");
Toast.makeText(getApplicationContext(), "Hello",10000).show();
}
};
将此替换为上述代码
Button btn=(Button)getActivity().findViewById(R.id.btnSend);
Log.v("HI","Button Created !!");
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//adapter=new ArrayAdapter<String> (getActivity(),android.R.layout.simple_list_item_1,listItems);
//setListAdapter(adapter);
Log.v("HI","Button !!");
Toast toast = Toast.makeText(getActivity().getBaseContext(), "Hello",10000);
toast.show();
}
};
btn.setOnClickListener(listener);