-1

I'm new to StackOverflow and also new to Android development, and I have run into quite an irritating issue with a certain app that I am developing. I have two buttons on my main activity, an "Add Customer" button and a "Show Customers" button, each leading to their own respective activities.

At first, only the Add Customer button would work and successfully go to its activity with no issues. The Show Customers button would cause the app to force close.

At this point, I had thought that my Show Customers button was not working correctly, so to test this I had switched the activities for which each button was leading to in order to see if the Add Customer button would again successfully lead to the Show Customers activity. Upon testing this, the Add Customer button actually now caused the app to force close and the Show Customers button worked and went to the Add Customer activity.

After completing this, I knew that there was something wrong with the actual Show Customers activity and not the Show Customers button itself, but I am completely stuck as to why it is not working. All activities are in the manifest. I believe there is something wrong with my Show Customer xml code. Below I will post all code I have for the app so far:

MainActivity.java

package bcs421.jorgeramirez.lab.layouts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {


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

    Button addCustButton = (Button)findViewById(R.id.button_add_customer);
    Button showCustButton = (Button)findViewById(R.id.button_show);
    addCustButton.setOnClickListener(this);
    showCustButton.setOnClickListener(this);
}

    @Override
    public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.button_add_customer:
            Intent intent1 = new Intent(MainActivity.this, AddCustomerActivity.class);
            startActivity(intent1);
            break;
        case R.id.button_show:
            Intent intent2 = new Intent(MainActivity.this, ShowCustomerActivity.class);
            startActivity(intent2);
            break;
        default:
            break;      
    }


}
    }

ShowCustomerActivity.java

package bcs421.jorgeramirez.lab.layouts;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ShowCustomerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_customer);
    ListView listNames;
    ArrayAdapter<String> listAdapter;
    listNames = (ListView)findViewById(R.id.listView);

    String [] names = new String[] {"Derek Jeter", "David Robertson", "Mark Texiera"};

    ArrayList<String> nameArray = new ArrayList<String>();
    nameArray.addAll(Arrays.asList(names));

    listAdapter = new ArrayAdapter<String>(this,R.layout.activity_show_customer, nameArray);



    listNames.setAdapter(listAdapter);


}



}

activity_show_customer.xml

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



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

    </ListView>

    <TextView   
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:padding="10dp"    
        android:textSize="16sp" >
    </TextView>



   </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bcs421.jorgeramirez.lab.layouts"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".AddCustomerActivity"
        android:label="@string/app_name" >

    </activity>

    <activity 
        android:name=".ShowCustomerActivity"
        android:label="@string/app_name" >

    </activity>

</application>

</manifest>

I know I should post a logcat but I honestly don't know how to get to it and I have been testing the app on my phone since I can't get AVD to start up for some reason. I have searched the website and online for a solution but I cannot seem to find one. Any help would be GREATLY appreciated! Thanks in advance guys.

2 Answers2

0

One potential problem that I see is on the following line:

listAdapter = new ArrayAdapter<String>(this,R.layout.activity_show_customer, nameArray);

Here you've used R.layout.activity_show_customer which is the id of your layout file. You need to use an id of a resource which tells it the layout of a single item in the list view. Something like android.R.layout.simple_list_item_1. Checkout ArrayAdapter in android to create simple listview

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

Yes the problem is with your ShowCustomerActivity ..Your are passing the activity layout to the ArrayAdapter which is wrong

public class ShowCustomerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_customer);
    ListView listNames;
    ArrayAdapter<String> listAdapter;
    listNames = (ListView)findViewById(R.id.listView);

    String [] names = new String[] {"Derek Jeter", "David Robertson", "Mark Texiera"};

    ArrayList<String> nameArray = new ArrayList<String>();
    nameArray.addAll(Arrays.asList(names));

    listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, nameArray);
//The layout should be your list Item layout and not the activity layout.



    listNames.setAdapter(listAdapter);


}
Dominic D'Souza
  • 961
  • 2
  • 7
  • 16