Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Every time I use to run the application.It crashes.What could be the issue??can anyone help me out.I have called 2nd activity using current activity's onClickItem method but it's not working

What I have tried:

MainActivity

package com.example.brandonmain.listviewexample1;

import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static android.R.attr.name;

public class MainActivity extends ListActivity {

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

ListView resultsListView = (ListView) findViewById(R.id.results_listview);

HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("Chairman Seceretery", "7th floor");
nameAddresses.put("Corporate Affairs Division", "6th floor");
nameAddresses.put("Director(Technical) Secretariat", "5th Floor");
nameAddresses.put("Operations", "5th Floor");
nameAddresses.put("Emergency Control room", "5th Floor");
nameAddresses.put("Enterprise risk management", "5th Floor");
nameAddresses.put("Business Planning", "5th Floor");
nameAddresses.put("Director(Finance) Secretariat", "4rth Floor");
nameAddresses.put("Finance", "4rth Floor");
nameAddresses.put("DIRECTOR (PERSONNEL) SECRETARIAT", "3rd Floor");
nameAddresses.put("Personnel", "3rd Floor");
nameAddresses.put("ED(LAW)& PLO", "3rd Floor");
nameAddresses.put("Business Planning", "3rd Floor");
nameAddresses.put("Transformation monitoring office", "3rd Floor");
nameAddresses.put("Vigilance", "2nd Floor");
nameAddresses.put("Administration", "2nd Floor");
nameAddresses.put("Parliament Cell", "2nd Floor");
nameAddresses.put("Corporate Planning", "2nd Floor");
nameAddresses.put("Coal Import Group", "2nd Floor");
nameAddresses.put("Director(P&SP) Secretariat", "1st Floor");
nameAddresses.put("Business Planning", "1st Floor");
nameAddresses.put("C & IT", "1st Floor");
nameAddresses.put("Medical & Health Service", "1st Floor");
nameAddresses.put("Director(RM&L) Secretariat", "1st Floor");
nameAddresses.put("CRMG", "1st Floor");
nameAddresses.put("ICVL", "1st Floor");
nameAddresses.put("O/o ED(Forest)", "1st Floor");
nameAddresses.put("Reception", "Ground Floor");
nameAddresses.put("State Bank Of India", "Ground Floor");
nameAddresses.put("Library", "Ground Floor");
nameAddresses.put("Basement", "Ground Floor");
nameAddresses.put("Administration-Kaushambi ", "Ground Floor");
Map<String, String> treeMap = new TreeMap<String, String>(nameAddresses);


List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});


Iterator it = treeMap.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id)
{
super.onListItemClick(l, view, position, id);

if (position == 0) {
Intent intent = new Intent(this, Adminstrative.class);
startActivity(intent);
}
}





}

Mainxml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.brandonmain.listviewexample1.MainActivity">

<ListView
android:id="@+id/results_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>


I want my current listview to open a new listview when an item is pressed in current ListView.

MainActivity

package com.example.brandonmain.listviewexample1;

import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static android.R.attr.name;

public class MainActivity extends ListActivity {

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

ListView resultsListView = (ListView) findViewById(R.id.results_listview);

HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("Chairman Seceretery", "7th floor");
nameAddresses.put("Corporate Affairs Division", "6th floor");
nameAddresses.put("Director(Technical) Secretariat", "5th Floor");
nameAddresses.put("Operations", "5th Floor");
nameAddresses.put("Emergency Control room", "5th Floor");
nameAddresses.put("Enterprise risk management", "5th Floor");
nameAddresses.put("Business Planning", "5th Floor");
nameAddresses.put("Director(Finance) Secretariat", "4rth Floor");
nameAddresses.put("Finance", "4rth Floor");
nameAddresses.put("DIRECTOR (PERSONNEL) SECRETARIAT", "3rd Floor");
nameAddresses.put("Personnel", "3rd Floor");
nameAddresses.put("ED(LAW)& PLO", "3rd Floor");
nameAddresses.put("Business Planning", "3rd Floor");
nameAddresses.put("Transformation monitoring office", "3rd Floor");
nameAddresses.put("Vigilance", "2nd Floor");
nameAddresses.put("Administration", "2nd Floor");
nameAddresses.put("Parliament Cell", "2nd Floor");
nameAddresses.put("Corporate Planning", "2nd Floor");
nameAddresses.put("Coal Import Group", "2nd Floor");
nameAddresses.put("Director(P&SP) Secretariat", "1st Floor");
nameAddresses.put("Business Planning", "1st Floor");
nameAddresses.put("C & IT", "1st Floor");
nameAddresses.put("Medical & Health Service", "1st Floor");
nameAddresses.put("Director(RM&L) Secretariat", "1st Floor");
nameAddresses.put("CRMG", "1st Floor");
nameAddresses.put("ICVL", "1st Floor");
nameAddresses.put("O/o ED(Forest)", "1st Floor");
nameAddresses.put("Reception", "Ground Floor");
nameAddresses.put("State Bank Of India", "Ground Floor");
nameAddresses.put("Library", "Ground Floor");
nameAddresses.put("Basement", "Ground Floor");
nameAddresses.put("Administration-Kaushambi ", "Ground Floor");
Map<String, String> treeMap = new TreeMap<String, String>(nameAddresses);


List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});


Iterator it = treeMap.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View view, int position, long id)
{
super.onListItemClick(l, view, position, id);

if (position == 0) {
Intent intent = new Intent(this, Adminstrative.class);
startActivity(intent);
}
}





}

Mainxml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.brandonmain.listviewexample1.MainActivity">

<ListView
android:id="@+id/results_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
/>

2nd Activity: package com.example.brandonmain.listviewexample1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Adminstrative extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adminstrative);
ListView resultsListView1 = (ListView) findViewById(R.id.administrative);
HashMap<String, String> nameAddresses1 = new HashMap<>();
nameAddresses1.put("xyz", "7th floor");

Map<String, String> treeMap1 = new TreeMap<String, String>(nameAddresses1);


List<HashMap<String, String>> listItems1 = new ArrayList<>();
SimpleAdapter adapter1 = new SimpleAdapter(this, listItems1, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});


Iterator it = treeMap1.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap1 = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap1.put("First Line", pair.getKey().toString());
resultsMap1.put("Second Line", pair.getValue().toString());
listItems1.add(resultsMap1);
}
resultsListView1.setAdapter(adapter1);

}
}

administrative.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.brandonmain.listviewexample1.Adminstrative">


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/adminstrative"
android:fastScrollEnabled="true"/>
</LinearLayout
Posted
Updated 24-Feb-18 4:49am
Comments
David Crow 26-Feb-18 11:58am    
Consider using the actual Android forum for such a (lengthy) question and code dump. This Q&A section is for much shorter questions and answers.

The exception tells you exactly what the problem is. Read here for more.

1 solution

Logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brandonmain.listviewexample1/com.example.brandonmain.listviewexample1.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:418)
at android.app.Activity.setContentView(Activity.java:2190)
at com.example.brandonmain.listviewexample1.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5763) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 
Share this answer
 
Comments
Richard Deeming 26-Feb-18 11:38am    
If you want to update your question with missing information, click the green "Improve question" link and edit your question. DO NOT post your update as a "solution".

And while you're at it, format your code properly using the code button on the toolbar, and remove any parts of the code which are not relevant to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900