Likwidsage
Likwidsage

Reputation: 81

New to Android - Fragments

New to android programming. For the past 3 days I've been going through tutorials and feel like I get the major parts of it. Every time I try to run the following code, android causes it to force quit before even opening up. I've gotten other more simpler fragments to work with little effort, but this is confusing me. There is no error being printed at all, probably because it shuts down immediately. Any help would be much aprreciated.

EDIT: Using the debugger I was able to get an

 "InflateException: Binary XML file #9: Error inflating class fragment. 

It stops after the follwing code:

 lv = (ListView) view.findViewById(R.id.lvMain);

Frag_MainList.java

public class Frag_MainList extends Fragment implements OnItemClickListener, OnItemLongClickListener{

ListView lv;
ImageView ivAddNote;
Communicator communicator;
Adapter_SQL database = new Adapter_SQL(getActivity());
ArrayList<Element> list; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_mainlist,  container, false);    
    lv = (ListView) view.findViewById(R.id.lvMain);

    database.open();
    list = database.getChildrenLess();
    database.close();

    Adapter_List adapter = new Adapter_List(getActivity(), list);

    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);
    lv.setOnItemLongClickListener(this);
    return view;
}



@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}



@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    communicator.SendID(pos);
    return true;
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
    database.open();
    database.switchComplete(list.get(pos).id);
    database.close();
}

public void setCommunicator(Communicator communicator){
    this.communicator = communicator;

}

public interface Communicator{
    public void SendID(int index);
}
}

Activity_Main.java

public class Activity_Main extends Activity implements Frag_MainList.Communicator{

Frag_MainList frag1;
Frag_ListChildren frag_children;
FragmentManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = getFragmentManager(); 
    frag1 = (Frag_MainList) manager.findFragmentById(R.id.fMainList);
    frag1.setCommunicator(this);
}  


@Override
public void SendID(int index) {
     frag_children = (Frag_ListChildren) manager.findFragmentById(R.id.fListChildren);      
     if (frag_children != null && frag_children.isVisible()){
        frag_children.displayChildren(index);
     }else{
         Intent intent = new Intent("com.likwid.wishlist.LISTCHILDREN");
         intent.putExtra("id", index);
         startActivity(intent);
     }    
}
}

frag_mainlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/lvMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00BBFF"
android:orientation="horizontal"
android:id="@+id/main_layout" >

<fragment
    android:id="@+id/fMainList"
    android:name="com.likwid.wishlist.Frag_MainList"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

</LinearLayout>

Upvotes: 0

Views: 89

Answers (1)

Likwidsage
Likwidsage

Reputation: 81

I figured it out. It seems the problem was with the database. getActivity in Frag_MainList would be called before the activity was actually created. Putting getActivity in the the "onActivityCreated" method allowed for getActivity to be properly called and seemed to take away all the problems it was causing. I think the reason it was showing a fragment error is because the fragment was faulty, making inflating in it impossible. Thanks everyone!

Upvotes: 1

Related Questions