BST Kaal BST Kaal - 1 month ago 11
Android Question

My Fragment is not being attached to the Main Activity Class.

Taking my previous experiences, i am trying to list some custom list items using

BaseAdapter
, which uses two string from
array
defined in
string
values.xml My app is crashing saying that my fragment is not attached with activity.

MainActivity.java is :

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

MyListFragment myListFragment = new MyListFragment();
fragmentTransaction.replace(android.R.id.content, myListFragment);
fragmentTransaction.commit();

}

}


MyListFragment.java is :

public class MyListFragment extends ListFragment{

MyCustomAdapter mAdapter;
String[] title = getResources().getStringArray(R.array.list_items);
String[] details = getResources().getStringArray(R.array.sub_list_items);

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_view, container,false);

mAdapter = new MyCustomAdapter(title, details, getActivity());

ListView contListView = (ListView)rootView.findViewById(android.R.id.list);
contListView.setAdapter(mAdapter);

return rootView;
}
}


And MyCustomAdapter.java is:

public class MyCustomAdapter extends BaseAdapter {

private Context context;
String[] Title;
String[] Detail;

public MyCustomAdapter(String[] text, String[] text1,Context context) {
Title = text;
Detail = text1;
this.context = context;
}

public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}

public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

// variable to hold context


public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row;
row = inflater.inflate(R.layout.fragment_main, parent, false);
TextView title, detail;
title = (TextView) row.findViewById(R.id.textView1);
detail = (TextView) row.findViewById(R.id.textView2);
title.setText(Title[position]);
detail.setText(Detail[position]);

return (row);
}
}


LogCat report after crashing is:

05-25 16:18:46.481: E/AndroidRuntime(23629): FATAL EXCEPTION: main
05-25 16:18:46.481: E/AndroidRuntime(23629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.listeventhandling/com.android.listeventhandling.MainActivity}: java.lang.IllegalStateException: Fragment MyListFragment{40d6b5e8} not attached to Activity
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.os.Looper.loop(Looper.java:154)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread.main(ActivityThread.java:4945)
05-25 16:18:46.481: E/AndroidRuntime(23629): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 16:18:46.481: E/AndroidRuntime(23629): at java.lang.reflect.Method.invoke(Method.java:511)
05-25 16:18:46.481: E/AndroidRuntime(23629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-25 16:18:46.481: E/AndroidRuntime(23629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-25 16:18:46.481: E/AndroidRuntime(23629): at dalvik.system.NativeStart.main(Native Method)
05-25 16:18:46.481: E/AndroidRuntime(23629): Caused by: java.lang.IllegalStateException: Fragment MyListFragment{40d6b5e8} not attached to Activity
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.Fragment.getResources(Fragment.java:817)
05-25 16:18:46.481: E/AndroidRuntime(23629): at com.android.listeventhandling.MyListFragment.<init>(MyListFragment.java:12)
05-25 16:18:46.481: E/AndroidRuntime(23629): at com.android.listeventhandling.MainActivity.onCreate(MainActivity.java:18)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.Activity.performCreate(Activity.java:4531)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-25 16:18:46.481: E/AndroidRuntime(23629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
05-25 16:18:46.481: E/AndroidRuntime(23629): ... 11 more

Answer

You have to mind the fragment lifecycle: onCreateView is before onActivityCreated which means the activity might not be there, so providing the activity withgetActivity() in the adapter in your onCreateView gives you this exception.

activity diagram

http://developer.android.com/guide/components/fragments.html

Comments