Android listview unfortunately, app has stopped - android

I have an issue about listview filled with string item althouh I look up a search about it. I don't undertand where the problems comes from though I define layout.xml correctly. How can I solve it out?
public class A extends AppCompatActivity {
ArrayList<String> items= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
items = new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);
listView.setAdapter(itemsAdapter);
}
a.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listviewAttactivePlaces"
tools:context="com.example.android.turkeytourguide.AttactivePlacesActivity">
</ListView>

you have to declare your arrayList as follows
List<String> items = new ArrayList<>();

please add this..
items=new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");

Try changing the type in declaration
public class A extends AppCompatActivity {
List<String> items= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
items = new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);
listView.setAdapter(itemsAdapter);
}

Related

Android Application gets crashed

The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!
I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}

ListView android and null pointer exception

I'm trying to show a simple ListView ,but I have some problems .It seems that I had a NullPointerException .
Here's the XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView"
/>
and my main class :
public class MainActivity extends Activity {
private List<String> exemple;
private ListView liste=(ListView) findViewById(R.id.listView);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exemple=new ArrayList<String>();
exemple.add("Item 1");
exemple.add("Item 2");
exemple.add("Item 3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, exemple);
liste.setAdapter(adapter);
}
}
I got this error :
07-07 11:59:27.024 2758-2758/tunisia_sat.com.essai E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: tunisia_sat.com.essai, PID: 2758
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tunisia_sat.com.essai/tunisia_sat.com.essai.MainActivity}: java.lang.NullPointerException
Move liste initialization after setContentView:
setContentView(R.layout.activity_main);
liste = (ListView) findViewById(R.id.listView);
Why NullPointerException?
You are calling ssetAdapter on null reference liste in the following line:
liste.setAdapter(adapter);
liste is null because it is being initialized before view is up(binding activity with view). So, findViewById returns Null.
Do not initialize ListView in declaration. Just do this:
public class MainActivity extends Activity {
private List<String> exemple;
private ListView liste;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
liste = (ListView) findViewById(R.id.listView);
exemple=new ArrayList<String>();
exemple.add("Item 1");
exemple.add("Item 2");
exemple.add("Item 3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, exemple);
liste.setAdapter(adapter);
}
}
You should Cast your listview after setting contentView int your onCreate method:
so you should do this:
outside your method, declare your ListView.
ListView listView;
So then cast in onCreate method:
listView=(ListView) findViewById(R.id.listView);
Regards.
Write the following statement
ListView liste=(ListView) findViewById(R.id.listView);
in Oncreate() method after
setContentView(R.layout.activity_main);
Example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView liste=(ListView) findViewById(R.id.listView);
exemple=new ArrayList<String>();
exemple.add("Item 1");
exemple.add("Item 2");
exemple.add("Item 3");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, exemple);
liste.setAdapter(adapter);
}

Changing extended ListView to Custom ListView

My MainActivity extends ListActivity, but now I want to add an ActionBar and this Activity should extend ActionBarActivity, to do that I need to change extended ListView to a custom ListView. I've tried many times, but unsuccessful. How can I do it?
Here's part of my MainActivity Code:
public class MainActivity extends ListActivity {
.
.
.
// Hashmap for ListView
ArrayList<HashMap<String, String>> testList;
#Override
protected void onCreate(Bundle savedInstanceState) {
testList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, testList,
R.layout.list_item, new String[] { TAG_NAME, TAG_DESCRICAO }, new int[] { R.id.name,
R.id.descricao });
setListAdapter(adapter);
}
}
Yes, go ahead and extend ActionBarActivity. Then, define your ListView in a layout file. For example, your layout will look something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ListView
android:id="#+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
Now, in your activity, you set the content view to the layout we created above. Next, get a reference to the ListView with findViewById. Finally, bind your adapter to the ListView. By the way, an ArrayAdapter may suit you better.
Code:
public class ListTestActivity extends ActionBarActivity {
ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_test);
mListView = (ListView) findViewById(R.id.lv_list);
mListView.setAdapter(new SimpleAdapter(
MainActivity.this, testList,
R.layout.list_item, new String[] { TAG_NAME, TAG_DESCRICAO }, new int[] { R.id.name,
R.id.descricao });
}
}
Try including super.onCreate(savedInstanceState) prior to everything else you have within the onCreate.

add item in to Listview Android?

I am trying to add an item to ListView:
correct?
public class Main extends Activity {
ArrayAdapter <String> listAdapter ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.listView);
String[] S= new String[]{"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(S) );
listAdapter = new ArrayAdapter<String>(this,R.layout.main,planetList);
list.setAdapter(listAdapter);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Main layout
String[] s = {"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA- Ali","YA ali","YA-Ali"};
list = (ListView)findViewById(R.id.listView); // Your activity should have listview with id listView
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); //android.R.layout.simple_list_item_1 is built-in xml file
list.setAdapter(listAdapter);
}

Simple List View in Android crashes if ArrayList is not empty

I have a Problem with a ListView.
Here is my code:
private void setViewProtocol() {
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.protocol, logs);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
It crashes - but when I remove the line logs.add("TEST 1") it works.
Can anyone help me? Why does it work when I call it with an empty list?
Thanks
Use the following:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, logs);
If you give a custom layout you have to have an TextView with the ID android.R.id.text1 or you have to override the getView()
Try this way,hope this will help you to solve your problem.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, logs);
ListView lv = (ListView) findViewById(android.R.id.listView1);
lv.setAdapter(adapter);
}

Categories

Resources