Caden Ratcliff
Caden Ratcliff

Reputation: 127

Source Not Found main.xml Beginning Android 4 Application Development by Wei-Meng Lee, Eclipse

I am beginning android 4 application development and in fact going through a book that is teaching me android. I am an chapter 4 and seem to have run into a problem that's been driving me insane. I am making a simple android app that displays presidents in a checkbox list and when you click a button, it will displayed what you have picked. It does not give me a chance though because it gives me the error "Source Not Found" right when I open the app on the emulator. Here is my code for the app.

package net.learn2develop.Basicviews5;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class BasicViews5Activity extends ListActivity {

    String[] presidents;

/** called when there activity is first created */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView lstView = getListView();
    // lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
    //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lstView.setTextFilterEnabled(true);

    presidents = 
            getResources().getStringArray(R.array.presidents_array);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_checked, presidents));
}

public void onListItemClick(
        ListView parent, View v, int position, long id)
{
    Toast.makeText(this,
            "You have selceted " + presidents[position],
            Toast.LENGTH_SHORT).show();
}

public void onClick(View view) {
    ListView lstView = getListView();

    String itemsSelected = "Selcted items: \n";
    for (int i=0; i<lstView.getCount(); i++) {
        if (lstView.isItemChecked(i)) {
            itemsSelected += lstView.getItemAtPosition(i) + "\n";
        }
    }
    Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.basic_views5, menu);
    return true;
}

}

Here is my code for the main.xml file I invoke. (or try to anyways.)

<?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:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".BasicViews5Activity" >

<Button 
    android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Show selceted items"
    android:onClick="onClick" />

<TextView
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

And here is my strings.xml file that invokes the presidents strings to put in the list.

<?xml version="1.0" encoding="utf-8"?>

<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="app_name">BasicViews5</string>
<string-array name="presidents_array">
    <item>Dwight D. Eisenhower</item>
    <item>John F. Kennedy</item>
    <item>Lyndon B. Johnson</item>
    <item>Richard Nixon</item>
    <item>Gerald Ford</item>
    <item>Jimmy Carter</item>
    <item>Ronald Reagen</item>
    <item>George H. W. Bush</item>
    <item>Bill Clinton</item>
    <item>George W. Bush</item>
    <item>Barack Obama</item>
</string-array>

</resources>

I really dont know whats going on here. I did some investigating and I narrowed the problem it seems to one line of code.

setContentView(R.layout.main);

I take it out, its fine, But I don't have the button that displays what you have selected. I put it in and it goes berserk and says source not found and the emulator is blank. I know that my post is long but thank you so much for taking the time to read it! Any help you can give me will be much appreciated!

Upvotes: 0

Views: 190

Answers (2)

codeMagic
codeMagic

Reputation: 44571

Converting comments to answer:

You are calling getListView() but giving an id to TextView of android:id="@+id/android:list" which the app is looking for. You only call getListVivew() if you haven't declared one in your xml, then it will by default create the ListView.

Change this line

ListView lstView = getListView();

to

ListView lstView = (ListView) findViewById(R.id.listView);

Then change your TextView in xml to ListView

Now it is looking for a ListView with the proper id. This also allows you to format your ListView further by using styles and themes if you wish to change the appearance

Upvotes: 1

chRyNaN
chRyNaN

Reputation: 3692

On page 192 (Chapter 4) in the book it has the following code snippet in the onCreate method:

//---no need to call this---
//setContenetView(R.layout.main);

That's because you are using ListActivity which is going to make the content view a ListView. If you wanted to have a custom layout with other views defined: make your class extend only Activity. Then in your main.xml file you can put something like this:

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

Now, in you onCreate method you can call: setContentView(R.layout.main) and below that reference the listview:

ListView list = (ListView) findViewById(R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));

Upvotes: 0

Related Questions