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 コードにコメントすると、アプリケーションは問題なく動作します。