Cherry
Cherry

Reputation: 173

App Crash when I Add a Button - has both the Button AND a listview in Activity

My application has one activity, and its corresponding java code extends Activity. The XML has a button and a listview inside it. Each button can be clicked to go to another activity (which I have not finished that part yet).

99% of my code works, except when I "findViewById" my button, the application completely crashes. If I erase this line, then the application works fine (except for the fact that my button would then be useless).

My 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" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageButton 
            android:id="@+id/bAddLexicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/new_pic"
            android:contentDescription="New Lexicon"/>


    </LinearLayout>

    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"></ListView>

</LinearLayout>

My Java Code:

package com.example.lexicav1;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


public class ListLexica extends Activity {

    ListView listview;
    Button createLexicon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlexica);

        listview = (ListView) findViewById(android.R.id.list);

            //THE FOLLOWING LINE MAKES MY APP CRASH,
            //IF I ERASED THIS ONE LINE IT WOULD WORK BUT THEN
            //I WOULD NOT BE ABLE TO DO ANYTHING WITH THE BUTTON???
        createLexicon = (Button) findViewById(R.id.bAddLexicon);

        List<String> items = new ArrayList<String>();
        items.add("Lexicon 1");
        items.add("Lexicon 2");
        items.add("Lexicon 3");
        ArrayAdapter<String> adapter =
                  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                //to change later
                Intent intent = new Intent("android.intent.action.booklistactivity");
                startActivity(intent);
            }
        });

    }       

}

Upvotes: 1

Views: 97

Answers (2)

Yogesh Lakhotia
Yogesh Lakhotia

Reputation: 888

Type cast button to ImageButton

Current you are casting to Button

Upvotes: 1

bricklore
bricklore

Reputation: 4165

you've got an ImageButton not a Button
so cast and declare it as a ImageButton too:

package com.example.lexicav1;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


public class ListLexica extends Activity {

    ListView listview;
    ImageButton createLexicon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlexica);

        listview = (ListView) findViewById(android.R.id.list);

            //THE FOLLOWING LINE MAKES MY APP CRASH,
            //IF I ERASED THIS ONE LINE IT WOULD WORK BUT THEN
            //I WOULD NOT BE ABLE TO DO ANYTHING WITH THE BUTTON???
        createLexicon = (ImageButton) findViewById(R.id.bAddLexicon);

        List<String> items = new ArrayList<String>();
        items.add("Lexicon 1");
        items.add("Lexicon 2");
        items.add("Lexicon 3");
        ArrayAdapter<String> adapter =
                  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                //to change later
                Intent intent = new Intent("android.intent.action.booklistactivity");
                startActivity(intent);
            }
        });

    }       

}

Upvotes: 0

Related Questions