I'm working on an app and I've been working on a "post" service where you can write a post and display it on a feed screen made of listview.
Now everything seem to be fine but I don't know why it's crashing before even starting.
Here is the code:
Post class:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Post extends ActionBarActivity {
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="#+id/input"
input = (EditText) findViewById(R.id.input);
// android:id="#+id/list"
startActivityForResult(new Intent(Post.this , MainActivity.class), 1);
}
public void addToList(View v) {
Editable text = input.getText();
Intent intent = new Intent();
intent.putExtra("result", text);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
in this class I have an edit text and abutton once the button is pressed I'm supposed to get the text in edit text and post in the listView of the other class
Main:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView list;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id2 == R.id.action_cart) {
startActivity(new Intent(MainActivity.this, Post.class));
}
return super.onOptionsItemSelected(item);
}
}
And the xml files are:
Post:
<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"
tools:context="com.example.ali.test1.Post">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="#id/input"/>
</RelativeLayout>
Main:
<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"
tools:context="com.example.ali.test1.MainActivity">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
any help would be appreciated I've been stuck here for too long.
Thank You.
You are trying to add adapter to a listview which is null, that's why you must be getting NullPointerException. You need to initialize it before you can use it.
You need to add
list= (ListView) findViewById(R.id.mylistview); // replace R.id.mylistview with the id of your listview in layout activity_main
after
setContentView(R.layout.activity_main);
in onCreate method of MainActivity
Change your MainActivity .Because you not initialize Listview properly.
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.list1);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
First, sorry for my english.
There is problem with android app going to background, where OS release memory for other functions on OS or other application, when it need's memory. There release your object and you have NullPointerException on resume.
How fix this? OnResume event check object value and if it is null, create object.
How test this? Kill process in Android Device Monitor
Related
I'm working on a posting system in an app.
What I want to do is when I press on the post icon I'll go to another Activity where I have an EditText and a Button.
When I press the button I want to get the text from the EditText and post it in the ListView of the main page or the "feed" page.
I don't seem to know how to link the array adapter to the feed so that the text appears on the ListView.
This is the code:
Main where I need to know what to do:
package com.example.ali.test1;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
EditText input;
ListView list1;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id2 == R.id.action_cart) {
startActivity(new Intent(MainActivity.this, Post.class));
}
return super.onOptionsItemSelected(item);
}
}
This is the post activity :
package com.example.ali.test1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Post extends ActionBarActivity {
ArrayAdapter<String> adapter1;
EditText input;
ListView list;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="#+id/input"
input = (EditText) findViewById(R.id.input);
// android:id="#+id/list"
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
}
// android:onClick="addToList"
public void addToList(View view) {
adapter.add(input.getText().toString());
adapter.notifyDataSetChanged();
// Clear the input
input.setText("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_post, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
These are xml:
<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"
tools:context="com.example.ali.test1.MainActivity">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
post:
<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"
tools:context="com.example.ali.test1.Post">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/list" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="#id/input"/>
</RelativeLayout>
Thank you!
You need to use the method startActivityForResult(new Intent(MainActivity.this, Post.class),1);
in the post activity
public void finish (View v){
String text = input.getText();
Intent intent = new Intent();
intent.putExtra("result",text);
setResult(Activity.RESULT_OK,intent);
finish();
}
and in the main activity you need to define a list for arrayAdapter and add the result to the arrayList
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
list.add(result);
adapter.notifyDataSetChanged();
}
}
}
If the post activity is opened from the main activity - then the sensible way to do this here is to use "startActivityForResult". Your main activity should start your post activity and receive the result in
it's all here in the documentation : http://developer.android.com/training/basics/intents/result.html
You can pass the text value from the post activity back to the main activity in the extras bundle passed to onActivityResult.
I'm working on an app where I have the option of "posting", when you press the post button you go to another activity where we have an EditText and a Button, once the Button is pressed the text should be taken and "posted" in the feed page in a ListView.
Now when I press the Button everything is working fine but nothing appears in the listview.
Here's the code:
Post:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
public class Post extends ActionBarActivity {
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
// android:id="#+id/input"
input = (EditText) findViewById(R.id.input);
}
public void addToList(View v) {
Editable text = input.getText();
Intent intent = new Intent();
intent.putExtra("result", text);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
The class where the message should be seen:
package com.example.ali.test1;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
ListView list;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
list.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
adapter.add(result);
adapter.notifyDataSetChanged();
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
int id2 = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id2 == R.id.action_cart) {
startActivityForResult(new Intent(MainActivity.this , Post.class), 1);
}
return super.onOptionsItemSelected(item);
}
}
The xml files:
Post:
<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"
tools:context="com.example.ali.test1.Post">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList"
android:layout_below="#id/input"/>
</RelativeLayout>
Main:
<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"
tools:context="com.example.ali.test1.MainActivity">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
</RelativeLayout>
The problem is that nothing is showing in the listview
any help?
Your not sending your intent.
startActivity(intent);
Then you are not receiving your intent on the other end properly.
You are using the startActivityForResult(); strategy.
In your second activity call the getIntent() method to retrieve your intent and data.
I am trying to parse JSON data from an online source. I used debugging and saw that JSON data was returned just fine to the activity. However, when I run the app, the activity where this data is supposed to appear, it appears blank. I suppose there is something wrong with how I am inflating the list with this data.
The tutorial I am using is: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Please help me fix this.
Here is the activity code:
package com.plusworks.appdev.youcancook;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class CuisineDishes extends BaseActivity {
private ProgressDialog pDialog;
ListView dishList;
ArrayList<HashMap<String,String>> cuisineDishList;
private static String url = "http://youcancook.site90.com/amdishes.json";
JSONArray dishes=null;
private static final String ARRAY_DISHES="americandishes";
private static final String ID="id";
private static final String NAME="name";
private static final String SHORTDESC="shortdesc";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout fl = (FrameLayout) findViewById(R.id.main_content);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_cuisine_dishes, null, false);
fl.addView(contentView);
Intent mIntent = getIntent();
String cuisineName = mIntent.getStringExtra("CuisineName");
getSupportActionBar().setTitle(cuisineName + " Dishes");
cuisineDishList=new ArrayList<>();
dishList = (ListView) findViewById(R.id.dish_list);
new AsyncDishModel().execute();
}
private class AsyncDishModel extends AsyncTask<Void,Void,Void>{
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog=new ProgressDialog(CuisineDishes.this);
pDialog.setMessage("Fetching dishes...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
ServiceHandler sh=new ServiceHandler();
String jsonStr=sh.makeServiceCall(url, ServiceHandler.GET);
if(jsonStr!=null){
try{
JSONObject jObject=new JSONObject(jsonStr);
dishes=jObject.getJSONArray(ARRAY_DISHES);
for(int i=0;i<dishes.length();i++){
JSONObject c=dishes.getJSONObject(i);
String id=c.getString(ID);
String name=c.getString(NAME);
String shortdesc=c.getString(SHORTDESC);
HashMap<String,String> templist=new HashMap<String,String>();
templist.put(ID,id);
templist.put(NAME,name);
templist.put(SHORTDESC,shortdesc);
cuisineDishList.add(templist);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
#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(
CuisineDishes.this, cuisineDishList,
R.layout.cuisine_dish_list_item, new String[] {ID,NAME,
SHORTDESC}, new int[]{ R.id.mId,R.id.title_dish,
R.id.desc_dish});
dishList.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_cuisine_dishes, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The service handler is the same as given in the tutorial.
Here is my activity layout code:
<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: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.plusworks.appdev.youcancook.CuisineDishes">
<ListView
android:id="#+id/dish_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
Here is the code for cuisine_dish_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/title_dish"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Title"
android:textSize="30dp" />
<TextView
android:id="#+id/desc_dish"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Short Description"
android:textSize="20dp" />
<TextView
android:id="#+id/mId"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="ID"
android:visibility="invisible" />
</LinearLayout>
You have to make custom adapter by extending your class with BaseAdapter class and then set your customer adapter to dishlist.
for custom adapter example you can follow http://www.vogella.com/tutorials/AndroidListView/article.html
I've been having issues when trying to setVisibility to a spinner. Basically there are two spinners. The first spinner has the options yes and no. When the program starts the default option will be yes and the second spinner should be invisible. When the user selects no on the first spinner then the second spinner becomes visible. When I comment out the code for visibility the program compiles on the emulator.
I don't know why the app keeps crashing as soon as i try to make the spinner visible or invisible. Any help would be much appreciated. I thank you in advance for your time and effort.
This is my main.xml
<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" tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView android:text="#string/CO" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/co" />
<Spinner
android:id="#+id/attendance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/Attendance"
android:prompt="#string/present" />
<Spinner
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/Status"
android:prompt="#string/status" />
</LinearLayout>
This is strings.xml
<resources>
<string name="app_name">Parade State Gen</string>
<string name="CO">CO</string>
<string name="present">Present?</string>
<string name="status">Status</string>
<string name="action_settings">Settings</string>
<string-array name="Attendance">
<item>Yes</item>
<item>No</item>
</string-array>
<string-array name="Status">
<item>LL</item>
<item>OL</item>
<item>PCL</item>
<item>CCL</item>
<item>O/S</item>
<item>CNB</item>
<item>TNB</item>
<item>MC</item>
<item>RSO</item>
<item>RS</item>
<item>OC</item>
<item>AS</item>
<item>AO</item>
</string-array>
This is the main class
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
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.Spinner;
import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends ActionBarActivity {
Spinner attendance, status;
private Button btnSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnAttendance();
//addListenerOnButton();
}
public void addItemsOnAttendance() {
attendance = (Spinner) findViewById(R.id.attendance);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Attendance, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
attendance.setAdapter(adapter);
attendance.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String myData = attendance.getSelectedItem().toString();
int pos = adapter.getPosition(myData);
if (pos == 1) {
status.setVisibility(View.INVISIBLE);
//Toast.makeText(getBaseContext(), myData + " already in Spinner", Toast.LENGTH_LONG).show();
// addListenerOnSpinnerItemSelection();
} else {
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
public void addListenerOnSpinnerItemSelection () {
status = (Spinner) findViewById(R.id.status);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Status, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
status.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You initialize spinner variable after call setVisiblity, that's why you receiving NullPointerException. It should be something like that:
addListenerOnSpinnerItemSelection();
status.setVisibility(View.INVISIBLE);
Btw, read something about logcat - it impossible to do android apps without it.
Hi I am trying to make an app that shows a list of items using listView and when the user select one of the items from the list. the app will call the specific class that is link to the item selected however i encountered an error at the second #override, saying that the override must override a super class. Here are my codes for class and xml.
<?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:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/botanicgate" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package com.fyp.gulliver;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class HotSpot extends ListActivity{
/** Called when the activity is first created. */
String places[] = {"BotanicGarden", "Sentosa"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotspot);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter
(HotSpot.this, android.R.layout.simple_list_item_1,
places));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Class ourClass;
String item = ((TextView)view).getText().toString();
try {
ourClass = Class.forName
("com.fyp.gulliver." + item);
Intent ourIntent = new Intent(HotSpot.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
As i am still new to android, thus i do not know what mistakes i have made, I will be grateful if another one can help me to solve my error thank you :D
Try changing this:
listView.setOnItemClickListener(new OnItemClickListener();
to
listView.setOnItemClickListener(new AdapterView.OnItemClickListener();
and remove the 2nd #Override