I am creating an application that saves GPS coordinates; I have created a button which launches a new Activity to display, as a list all the save coordinates. The program crashes a soon as I hit the button, here is what I am doing.
class 1:
public void openLatLonData(View view)
{
Intent intent = new Intent(this, GpsDataList.class);
ArrayList<String> allLocalStrings = new ArrayList<String>();
for(int i =0; i< AllLocals.size();i++)
{
allLocalStrings.add(AllLocals.get(i).getLat());
allLocalStrings.add(AllLocals.get(i).getLon());
}
intent.putStringArrayListExtra("data", allLocalStrings);
startActivity(intent);
}
This method calls the new activity:
public class GpsDataList extends ListActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundleIn)
{
super.onCreate(bundleIn);
Intent intent = getIntent();
ArrayList<String> listData = new ArrayList<String>();
listData = intent.getStringArrayListExtra("data");
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listData));
getListView().setTextFilterEnabled(true);
//finish();
}
}
the button contains the following XML:
<Button android:layout_height="wrap_content"
android:text="Show Collected Data"
android:id="#+id/view_lat_lon_data"
android:layout_width="wrap_content"
android:onClick="openLatLonData">
</Button>
And I have added a new .xml file named gpsdatalist.xml under the res/layout folder:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
Lastly the manifest contains this: .
I believe I have all the pieces in play- Hopefully someones see the mistake
Thanks in advance guys!
Did you define the activity in the AndroidManifest.xml file?
Anyway, your best clue resides in the logcat of your exception. You will solve it in 2 seconds after cheking the logcat.
Related
I wanted to devise some intents between two activities. I passed the intent without declaring anything . I wanted to define Adapter to inflate listview in the intent but I came across the issue when the intent is opened . It is caused by listview. How can I solve it out?
Here are the code below.
public class AttactivePlacesActivity extends AppCompatActivity {
ArrayList<City> c = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
MainActivity m = new MainActivity();
c = m.mArraylist;
AAdapter r = new AAdapter(this,c,R.color.mainBackground);
ListView listView = (ListView) findViewById(R.id.aa);
listView.setAdapter(r);
}
}
R.layout.a
<?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/aa"
tools:context="com.example.android.tourguide.AttactivePlacesActivity">
</ListView>
gridview Intent
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"A",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,AttactivePlacesActivity.class);
startActivity(intent);
break;
}
}
});
You create a reference of MainActivity to get the mArrayList, don't you? Firstly, pls don't create any reference of an activity in another one. Secondly, if you want to get the mArraylist in the second activity, just put it in intent like this:
intent.putSerializable("My array",mArrayList);
and receive it by using:
c = getIntent.getSerializable("My array");
P/S: Some advices for you, please name the variable by the meaningful name, try to learn the OOP java carrefully before starting with Android. And the last one, searching before asking
Good day to everyone, I am new to Android, I keep on messing with ListView. The code below wont work! I dont know why. Please let me know where's the mistake.
Main Activity.java
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> data = new ArrayList<String>();
data.add("Follow Me!");
data.add("This is now.");
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
After running the above code, my adroid device will show a dialogue saying:
"Sorry!
The application CodeGenerator has stopped unexpectedly. System failed to repair the error. Please use other software."
Please help!
The setContentView(R.layout.activity_main); line should be immediately after super.onCreate(savedInstanceState);.
What this
setContentView(R.layout.activity_main);
line does is it basically displays the screen (your android xml to the user). Now when you do
ListView listView = (ListView) findViewById(R.id.listView);
You are trying to get your ListView from your xml in your java code. But if you dont set the content itself, then there is no ListView and hence your application is crashing.
Anyway, I changed the code, and its running now. Here's the code:
public class MainActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> data = new ArrayList<String>();
data.add("Follow Me!");
data.add("This is now.");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
setListAdapter(adapter);
}
}
Still a mystery for me, why my first code did not work! If anyone can explain that, please let me know! Thank you!
i want to do something that when i click on button he will openfor me something like listview with the name of all the contacts in my phone...how can i do it?
i know how to get the names of all the contacts in my phone and put them into string array,however how can i open a new windows with a listview of all the contacts name when i click on button?
thanks
In your first Activity when you click on button:
startActivity(new Intent(this, ContactsActivity.class));
Then in your ContactsActivity:
public class ContactsActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.contacts_view);
ListAdapter adapter = createAdapter();
setListAdapter(adapter);
}
/**
* Creates and returns a list adapter for the current list activity
* #return
*/
protected ListAdapter createAdapter()
{
// List with strings of contacts name
contactList = ... someMethod to get your list ...
// Create a simple array adapter (of type string) with the test values
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactList);
return adapter;
}
}
XML file for your ContactsActivity (name it to contacts_view.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
</LinearLayout>
Can use a listActivity
listActivity on android developer
On the button click you need to start new Activity(or ListActivity) that contains list of all contacts.So you need to set onClicklistener then in onClick function you need to write code for starting Activity
On second activity Create a CustomAdapter that will initialised with say List
class Contact
{
private String number;
private String name
}
Thanks.
I am trying to display a list after clicking a button on Android.
This is my code:
//Button after clicking should display a list
final Button button4 = (Button) findViewById(R.id.widget30);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), TestListActivities.class);
startActivity(myIntent);
}
public class TestListActivities extends Activity {
String[] listItems = {"Game1", "Game2",
"Game3", "Game4"};
public ListView la;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
la=(ListView)findViewById(R.id.widget50);
la.setAdapter(new ArrayAdapter<String>(this,
R.layout.main2, listItems));
}
}
My XML file is:
?xml version="1.0" encoding="utf-8"?
AbsoluteLayout
android:id="#+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
ListView
android:id="#+id/widget50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
AbsoluteLayoute
But when I click the button, the application is forced to close and it crashes.
Manifest file
<application android:icon="#drawable/icon" android:label="BAM! Pong" android:debuggable="true">
<activity android:name=".GUI" android:label="#string/app_name" android:screenOrientation="portrait"><intent-filter><action android:name="android.intent.action.MAIN"></action>
It's difficult to know without seeing the stacktrace. Run "adb logcat" from the command line while running your app or watch the logs in the DDMS perspective in Eclipse. The stacktrace will give you the location of the bug or reason for the FC.
You seem to use the R.layout.main2 both as the activity layout (setContentView(R.layout.main2);) and as the ListView layout. (a.setAdapter(new ArrayAdapter(this, R.layout.main2, listItems));) and that is not the right way to use the ListView, the Activity layout should be a layout that contains a ListView, while the layout you are giving to the ArrayAdapter should be a different layout containing the views you would like to be repeated for each entry in your list.
Take a look at the API demos in the android sdk, there are several ListView examples that show you how to use the ListView.
Have you declared Activity in AndroidManifest?
I am creating an android application in which there are two activites. One activity contains a button. On the button click, i want to switch to other activity which displays a list view containing few options.
Two switch between the screens or activities , i am using the following code
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
Since my Activity2 class extends the 'ListActivity', this code doesn't seem to work. On my button click, i want to display a list view containing some data.
Any help would be appreciated
#Siddharth
i seem to be doing almost the same thing
Here is my actual code
From Activity 1
public void onClick(View v) {
Intent intent = new Intent(View_Data.this,CategoryList.class);
startActivity(intent);
}
In Activity 2
public class CategoryList extends ListActivity {
public TextView selection;
public String[] categories;
ArrayList<String> type_of_category;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_list);
getCategories();
}
public void getCategories() {
DBHelper dbhelper = new DBHelper(this);
type_of_category = new ArrayList<String>();
type_of_category = dbhelper.getTypesOfQuotes();
String[] items = new String[100];
for(int i=0;i<type_of_type_of_category.size();i++)
{
items[i] = type_of_type_of_category.get(i);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}
}
Here is my XML File
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
In My 2nd Activity, i get the error in this line
setContentView(R.layout.category_list);
Depending on whether your data in the second activity is from a database or static data, this code should work for you. I am assuming from your post that you dont need to send data from the 1st activity to the 2nd activity. This is actual code from my application which is database driven. If you are not using a database, parts of this code can be changed to use that instead of a database. It should get you started:
From the 1st Activity:
public void onClickListContacts(View target)
{
Intent listContacts = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
startActivity(listContacts);
}
The 2nd activity:
public class ContactsList extends ListActivity {
DBAdapter dbContactList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_list);
dbContactList = new DBAdapter(this);
// Calls the method to display the ListView of all Contacts
fillData();
}
private void fillData() {
// Pull the data from the database
String[] fields = new String[] { dbContactList.TABLE_CON_NAME };
int[] views = new int[] { android.R.id.text1 };
Cursor c = dbContactList.getAllContacts();
startManagingCursor(c);
// Set the ListView
ListAdapter prjName = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, fields, views);
setListAdapter(prjName);
dbContactList.close();
}
For a static list, you can also refer to this tutorial: http://www.vogella.de/articles/Android/article.html#lists
Hope this helps.
PS: It would be great help if you could post code of the 2nd activity which has problems.
Add this to your XML and see if that helps:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>