Android app crashes with weird error - android

I have button that launches a ListActivity, but every time I click on it, the app crashes with the following LogCat error:
01-10 13:12:51.327: E/AndroidRuntime(2970): FATAL EXCEPTION: main
01-10 13:12:51.327: E/AndroidRuntime(2970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.Settings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
This is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background"
android:orientation="vertical"
android:padding="15dip" >
<ListView
android:id="#+id/listViewSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this is the Java code:
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Settings extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("foo");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
}
I have already tried changing the list id to list but if I do this I get the following error:
If I do this I get the following error: `Error: No resource found that matches the given name (at 'id' with value '#id/list').`

ListActivity does not require that you assign a layout to it via the setContentView() method, if you only want to show a ListView ListActivity contains by default a ListView.
In case you need to include more Views than a ListView in your ListActivity you can still assign a layout to your Activity. In this case your layout must contain a ListView with the android:id attribute set to #android:id/list.
So change your list id to:
android:id="#android:id/list"

This row in your XML:
android:id="#+id/listViewSettings"
...has to be like this:
android:id="#android:id/list"
Why? Well, the ListActivity requires you to use the id "android.R.id.list" if you want it to maintain your ListView (same thing applies when using a ListFragment).

Simply change your xml ListView-id
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
In this case your layout must contain a ListView with the android:id attribute set to #android:id/list

Related

TwoWayView setAdaptor is not allowing to set the adaptor(HorizontalListView)

I tried using TwoWayView. My xml contains
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rewardListView"
style="#style/TwoWayView"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tools:context=".RewardActivity" />
I have created the Adapter which extends RecyclerView.Adapter and properly implemented every methods. Now in the activity, on the instance of TwoWayView, I try to set the adapter, but its not allowing me to set it up.
mRewardListView = (TwoWayView) findViewById(R.id.rewardListView);
adaptor = new RewardListViewAdaptor(mRewardList, this);
mRewardListView.setAdapter(adaptor);
I am getting this error "setAdapter(android.widget.ListAdapter) in TwoWayView cannot be cast to RewardListViewAdaptor".
Any help will be highly appreciated.

How to handle empty ListView in android?

Having a bit of trouble dealing with empty ListView in android. Here's what I've got:
public class MainActivity extends ListActivity {
...
refreshList();
...}
public void refreshAlbumList(){
albumList=control.listAlbums();
if (albumList.length!=0){
ArrayAdapter<String> ap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,albumList);
setListAdapter(ap);
}else{
//deal with empty list somehow, currently results in error
}
}
What I've tried is:
this.getListView().setEmptyView(findViewById(android.R.id.empty));
And then inserting a TextView with id empty into main_activity.xml
But the result was that took over the screen and I couldn't see the list.
Current xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<ListView
android1:id="#id/android:list"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:layout_weight="1" >
</ListView>
</LinearLayout>
The ListView calls ListAdapter#isEmpty method to determine if it should show/hide itself and the empty view. You will therefore always need to set an ListAdapter.
So the answer is to always create your ArrayAdapter and always set it to the ListView. In your activities onCreate call ListView#setEmptyView method. Alternatively, you can simply have a TextView with the android:id="#android:id/empty" attribute and the ListActivity will detect it and assign it using the above method.
Example: https://gist.github.com/slightfoot/5519281

Android: Listview NullPointerException

I've written a simple android list view, but it is throwing a java.lang.NullPointerException during run time. Checked it in debug mode and it seems the findViewbyId is returning null.
So when I get to the setAdapter it throws the exception. But the list does exist in the R.java file and the main.xml. How can I fix this?
setContentView(my.namespace.R.layout.main);
String[] a={"asd","asdsad"};
ListView lt;
lt = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,my.namespace.R.layout.rowitem, a);
lt.setAdapter(adapter);
Main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Is your activity extends ListActivity? You dont need a separate layout if you extend ListActivity. See this tutorial for further reference.
Wirte this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,my.namespace.R.layout.simple_list_item_1, a);
Refer THIS

ListView not appearing in application

im trying to populate my listview beneath my tab function but for some reason its not appearing in my app. I Sampled the code in a external project and it worked so Im not sure why it is not appearing. Have I implemented the activity wrong in the manifest or do I have contrary codes. No errors or logcat issues when the button is selected. Its just a blank page beneath the tab. The page loads but with no list. Thanks
XML File
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
ListmoreActivity
package workout.fitty;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ListmoreActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, adobe_products));
}
}
The Activity in the manifest is added like;
<activity android:name=".ListmoreActivity" />
Just in case its wrong
You are wrong from the inception of the code refer the link below to learn listview in android...
http://www.vogella.de/articles/AndroidListView/article.html

Android Null Pointer Exception on OnClickListener line

I have a simple test application with a GridView containing an array of buttons, I'm trying to perform an operation (any operation) touching one of these buttons.
The main code is this:
package com.example.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
public class convert extends Activity {
//private ListView List;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String [] elenco = {
"ciao","questo","รจ","un esempio","Configurazione"
};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.oggetto,R.id.testogg,elenco);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(arrayAdapter);
Button btn = (Button) (findViewById(R.id.testogg));
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.oggetto);
}
});
}
}
The main.xml is the following:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
And oggetto.xml contains the following lines:
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="#+id/TextView01"
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/testogg">
</Button>
But when I try to run the application it return me a NullPointerException on the btn.setOnClickListener(new OnClickListener()) line.
May be that happen because the buttons have the same id? If this is the case, how can I obtain repeated buttons with different ids?
Thanks for your help!
May be that happen cause the buttons
have the same id
That could be. But it also shows something is wrong if that's even the case. Each button specified in XML is only created once. You can't create multiple buttons just by calling findViewById() multiple times or by passing in the resource ID.
But the problem comes because you call setContentView(R.layout.main);
findViewById() will ONLY return non-null views for views contained in main.xml. Your context knows nothing about your other XML layouts because they're not inflated.
You should probably start with simpler programs and go through the tutorials to get a feeling for how android views work.
What are you trying to accomplish? You're originally setting a layout that only contains a GridView, then you're trying to find a Button that doesn't even exist (at the faulting line, your content view is your main.xml document, which does not contain any buttons, let alone one with the id of testogg). This means that btn is null, as the button does not exist. That is the cause of your NullPointerException.
If you want to intercept a click event for an item in your GridView, you need to be using the OnItemClickListener, and attaching that to your GridView. See the link for the onItemClick method definition.

Categories

Resources