Eugênio Fonseca Eugênio Fonseca -4 years ago 94
Android Question

Drawer List Array Adapter crashes app

I'm facing a problem regarding the Navigation Drawer menu implementation for Android. Right when I attempt to populate the left menu options with a String list (or array, both do not work) through an String Adapter, the application crashes so fast that LogCat catches no messages.

The Main Activity erroneous part:

package edu.getjedi.frontend.mobile;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

import java.util.ArrayList;

public class MainMapActivity extends FragmentActivity implements OnMapReadyCallback {
private UserLocationHandler locationHandler;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ArrayList<String> test = new ArrayList<>();

test.add("abc");
test.add("def");
test.add("ghi");

setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerMenuHandler());
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, test)); // Trouble is here!!
}

(...)


If I comment the
mDrawerList.setAdapter(...)
line, everything else works just fine.




The layout/activity_main.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.getjedi.frontend.mobile.MainMapActivity" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>


The guide I followed:
Navigation Drawer - Android Developers

What's the difference between the tutorial and my code sample?
- The FrameLayout is a map fragment containing a pointer to MainMapActivity as a context.




The application handles GPS and Http requests through Volley just fine, but when I added this line

mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, test));


it refuses to boot and produce any useful message.

Any help appreciated.
Thanks for reading.

Answer Source

You use it completely wrong.

new ArrayAdapter<String>(CONTEXT,VIEW_ID_TO_SHOW_TEXT, YOUR_LIST)

You should not pass R.layout.activity_mainto it. use :

android.R.layout.simple_list_item_1

or make your custom layout which is just an Text view.

new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, test)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download