user8044729
user8044729

Reputation:

App Crashes When Navigaiton Drawer Item is Pressed

UPDATE : PROBLEM IS SOLVED

enter image description here

According to this image, I am trying to open fragment according to each drawer item. Currently I've made fragment only for 'Easy' which contain a ListView but my app CRASHES everytime I press it. I've Android Nougat (v7.0) running on my phone.

Here is my code ...

Fragment Layout (fragment_easy.xml) :

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="afm.pymonk.EasyFragment"
    android:id="@+id/easyFragment">

    <!-- TODO: Update blank fragment layout -->

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

</RelativeLayout>

Fragment JAVA Code inside onCreateView in EasyFragment.java :

View view = inflater.inflate(R.layout.fragment_easy, container, false);

String easyListContents[] = {"Introduction", "Hello World", "Operators", "Data Types",
            "Simple Arithmetic Operations", "Take input from user"};

ListView easyList = (ListView) view.findViewById(R.id.easyList);
ArrayAdapter<String> easyAdapter = new ArrayAdapter<String>(EasyFragment.this.getActivity(),
            android.R.layout.simple_spinner_dropdown_item, easyListContents);
easyList.setAdapter(easyAdapter);

easyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        return;
    }
});

return view;

Code to open fragemnt inside onNavigationItemSelected in main activity (Home.java) :

int id = item.getItemId();

if (id == R.id.icon_easy) {
    EasyFragment easyFragment = new EasyFragment();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.mainBase, easyFragment, easyFragment.getTag()).commit();
}

Device Error Log :

enter image description here

Upvotes: 0

Views: 84

Answers (2)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

Return view not return inflater.inflate(R.layout.fragment_easy, container, false); in your fragment in onCreateView method.

Edited :

Implement OnFragmentInteractionListener in MainActvity

Ex:

public class MainActivity extends AppCompatActivity
        implements OnFragmentInteractionListener {

// Implement the methods here
}

Upvotes: 2

Pavneet_Singh
Pavneet_Singh

Reputation: 37414

Your easyFragment.getTag() call will return null because there was no setTag call, so just use some String tag to bind it with your fragment instead ofnull

 manager.beginTransaction().replace(R.id.mainBase, easyFragment, "easyFrag").commit();
//                                                                 ^^^^^^

Update :

You are using interface listener callbacks in fragment but there is no implementation in your activity so make sure to do

class Home extends AppCompatActivity implements OnFragmentInteractionListener {
  //                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 // override methods of OnFragmentInteractionListener and provide their implementation
}

Upvotes: 1

Related Questions