Unable to start activity ComponentInfo.....java.lang.IllegalStateException: Already attached - java

I m getting that error ,as you can see i m adding the data that i grabbed from the first activity and storing it into the my array in the second activity .
Then i use the array to populate the list view .So the problem is whenever i click to the save button on the first view
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
super.onCreate(savedInstanceState);
// setContentView(Your_Layout);
//Bundle extras = null;
//if(getIntent().getExtras() != null){
// extras = getIntent().getExtras();
//}
Intent intent = getIntent();
// ListView lv = (ListView) findViewById(R.id.View);
String data = intent.getStringExtra("data");
String first = intent.getStringExtra("stringOne");
String second = intent.getStringExtra("stringTwo");
String Third = intent.getStringExtra("stringThree");
String Fourth = intent.getStringExtra("stringFour");
String Fifth = intent.getStringExtra("stringFive");
String Sixth = intent.getStringExtra("stringSix");
// Find the ListView resource.
ListView lv = (ListView) findViewById( R.id.View );
// Create and populate a List of planet names.
String[] dataUser = new String[] { first,second,Third,Fourth,Fifth,Sixth};
ArrayList<String> dataList = new ArrayList<String>();
dataList.addAll(Arrays.asList(dataUser));
// Create ArrayAdapter using the planet list.
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
// Set the ArrayAdapter as the ListView's adapter.
lv.setAdapter(listAdapter);
}
}

You have 2 super.onCreate(savedInstanceState); in your code, remove one of them.

So I did remove the extrat super.onCreate(savedInstanceState); that i had but it looks like one of my edit text is passing a nul value .So i went trough every line but as you can see all the edixtext are passing a diffrent of null
http://pastebin.com/mys3R8QQ

Related

How to add a new varible (from previous activity) to a list to display on listview in android studio?

public class Main2Activity extends AppCompatActivity {
ListView simpleList;
String[] thoughtList = {" "};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String newThought = intent.getStringExtra(MainActivity.EXTRA_TEXT);
thoughtList.add(newThought); //error comes herer hich wont allow add
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
simpleList.setAdapter(arrayAdapter);
}
}
that is my code on second page it brings over varible (saved as newThought) from the previous activity which i then want to add to thoughtList to display in ListView but won't let me add the new varible to the list. does my code need tweaking or should i start again to achieve varible brougth over from previous activity to add into list view on activity 2
Have you tried using an ArrayList instead of an string array?
String array has a fixed size when array list does not.
That may be your problem.

How to read context of two spinners and send both to next activity?

I have two spinners and I want to grab the values that the user chooses and send it to the next activity. My first activity is titled IO and I create the spinners and get the data from the selections in my onCreate.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_io);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner locationSpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(IO.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.busStops));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
locationSpinner.setAdapter(myAdapter);
location = locationSpinner.getSelectedItem().toString();
Spinner destinationSpinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(IO.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.busStops));
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
destinationSpinner.setAdapter(myAdapter2);
destination = destinationSpinner.getSelectedItem().toString();
}
I tried sending the data in another method titled sendRoutes where I created an intent but it didn't work and I was wondering how to do this.
Assuming that you want to open your activity on a button click then,
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String destination = destinationSpinner.getSelectedItem().toString();
String location = locationSpinner.getSelectedItem().toString();
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtra("destination",destination);
intent.putExtra("location",location);
startActivity(intent);
}
});
You are passing destination and location values to NextActivity by intent.
in IO Activity definition Intent :
Intent intent = new Intent(IO.this,SecondActivity.class);
location = locationSpinner.getSelectedItem().toString();
destination = destinationSpinner.getSelectedItem().toString();
intent.putExtra(KEY_DEST,destination);
intent.putExtra(KEY_LOC,location);
startActivity(intent);
in SecondActivity get two parameters in onCreate :
String destination = getInetent().getExtras.getString(KEY_DEST);
String location = getInetent().getExtras.getString(KEY_LOC);

How to Save to an Array and display array in listview

I use this code to save the contents of a EditText (name) to an array, i'm wanting to then display this array in a list view on another activity. any idea? thanks
SingleItemView:
ArrayList<String> addArray = new ArrayList<String>();
Intent intent = new Intent(SingleItemView.this, mygardenMain.class);
Bundle bundle =new Bundle();
public void onClick(View v) {
String getInput = txt.getText().toString();
if (addArray.contains(getInput)){
Toast.makeText(SingleItemView.this,"Plant Already Added",Toast.LENGTH_SHORT).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(SingleItemView.this,"No Plant Selected",Toast.LENGTH_SHORT).show();
}
else
{
addArray.add(getInput);
Toast.makeText(SingleItemView.this,"Plant Added",Toast.LENGTH_SHORT).show();
bundle.putStringArrayList("addArray",addArray);
intent.putExtras(bundle);
startActivity(intent);
mygardenMain code:
public class mygardenMain extends Activity {
private ListView list;
private ArrayList<String> addArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygarden_list);
list = (ListView) findViewById(R.id.mygardenlist);
Bundle bundle = getIntent().getExtras();
ArrayList<String> addArray = bundle.getStringArrayList("addArray");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mygardenMain.this, android.R.layout.simple_list_item_1, addArray);
list.setAdapter(adapter);
}
}
Use Parcelable to send and receive data between activities.
To send
intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
startActivity(intent);
To receive,
getIntent().getParcelableArrayListExtra("key");
finally set listview adapter.
public class BasicViews5Activity extends ListActivity {
String[] presidents = {
“Dwight D. Eisenhower”,
“John F. Kennedy”,
“Lyndon B. Johnson”,
“Richard Nixon”,
“Gerald Ford”,
“Jimmy Carter”,
“Ronald Reagan”,
“George H. W. Bush”,
“Bill Clinton”,
“George W. Bush”,
“Barack Obama”
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---no need to call this---
//setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick(
ListView parent, View v, int position, long id)
{
Toast.makeText(this,
“You have selected “ + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
hope this helps
the String{} presidents is the array
I think this is what you want:
from:
https://developer.android.com/training/basics/firstapp/starting-activity.html
Build an Intent
An Intent is an object that provides runtime binding between separate components (such as two activities). The Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but in this lesson, your intent starts another activity.
In MainActivity.java, add the code shown below to sendMessage():
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
This is how you pass String in android to the next activity.
This could be adapted to add an array or values in an array potentially as and object or as string through a for loop.
try this.
first convert ArrayList to array and then pass to next activity
String [] arrrayData = addArray.toArray(new String[addArray.size()]);
get like this
String[] array;
Intent intent = new Intent(MainActivity.this,MyDrawing.class);
Bundle bundle =new Bundle();
bundle.putStringArray("array",array);
intent.putExtras(bundle);
startActivity(intent);
100% working
please be specific with your question.Secondly ListView is outdated,my advise to you is to use Recycler view
private RecyclerAdapter ra;
private ArrayList<ModalClassName> yourarraylistname;
recyclerView = (RecyclerView) findViewById(R.id.recyclerdown);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
yourarraylistname =new ArrayList<ModalClassName>();
//make sure yourarraylistname has some value
ra = new DownloadAdapter(this,yourarraylistname);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(ra);
Please check my ListView Example project on github. Here is the link
https://github.com/SanjayKushwah0601/ListView-Example
You can pass the ArrayList via intent
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putStringArrayListExtra("list", addArray);
startActivity(intent);
And then you can fetch this ArrayList on another screen
// Now fetch the contents from intent
addArray = getIntent().getStringArrayListExtra("list");
ArrayAdapter<String> adapter = new ArrayAdapter<>(ListActivity.this, android.R.layout.simple_list_item_1, addArray);
list.setAdapter(adapter);
Use Below Code to do this. to send String array from one Activity to next Activity.
String[] stringArray = new String[10];
Intent intent = new Intent(MainActivity.this,MyDrawing.class);
Bundle bundle =new Bundle();
bundle.putStringArray("stringArray",stringArray);
intent.putExtras(bundle);
startActivity(intent);
And Retrive Array in Next Activity from Intent like below:
Bundle bundle = getIntent().getExtras();
String[] stringArray = bundle.getStringArray("stringArray");
You can pass Array List Directly to Intent
ArrayList<String> arraylist = new ArrayList<String>();
Intent intent = new Intent(MainActivity.this, MyDrawing.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("arraylist",arraylist);
intent.putExtras(bundle);
startActivity(intent);
And Retrieve like below:
Bundle bundle = getIntent().getExtras();
ArrayList<String> arraylist = bundle.getStringArrayList("arraylist");

Unable to Pass an ArrayList of JSONObjects to new Activity

I am having trouble passing an ArrayList<JSONObject> to a new activity.
In my Search activity I use
intent.putParcelableArrayListExtra("data", resultsArray);
But I get a Wrong Argument error.
I used this SO question as a reference.
Intent.putExtra List
public class SearchActivity extends AppCompatActivity {
List<JSONObject> resultsArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
//other stuff
resultsArray = new ArrayList<JSONObject>();
}
public void goToActivity(){
Intent intent = new Intent(SearchActivity.this, SearchResultsListActivity.class);
intent.putParcelableArrayListExtra("data", resultsArray);
startActivity(intent);
}
// ...
}
My SearchResultsListAcivity is just a list
public class SearchResultsListAcivity extends AppCompatActivity {
public ArrayList<JSONObject> searchResultsArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
// .. .
searchResultsArray = getStringArrayListExtra("data");
ArrayAdapter<JSONObject> adapter = new ArrayAdapter<JSONObject>(this, android.R.layout.simple_list_item_1, android.R.id.text1, searchResultsArray);
ListView lv = (ListView) findViewById(R.id.listViewSearchResults);
lv.setAdapter(adapter);
}
}
I also have toyed with the implmentation answer listed here How to pass ArrayList of Custom objects to new activity?:
Intent intent = new Intent(getApplicationContext(), displayImage.class);
Bundle bundleObject = new Bundle();
bundleObject.putSerializable("KEY", arrayList);
intent.putExtras(bundleObject);
But again I get wrong argument error.
I would just transform your JSON array into a String array and then pass that to your next activity using putStringArrayListExtra(). You can decode it back to JSON array in your SearchResultsListActivity activity.
i) Transform your JSON array (or list) into a String array
JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(arr.getJSONObject(i).toString());}
ii) Pass it to your SearchResultsListActivity activity as extra
Intent intent = new Intent(SearchActivity.this, SearchResultsListActivity.class);
intent.putStringArrayListExtra("string_array", list);
startActivity(intent);
iii) Finally decode it in your next activity
Bundle stringArrayList = getIntent().getExtras();
ArrayList<String> list = stringArrayList.getStringArrayList("string_array");
JSONArray arr = new JSONArray(list);
I have not tried this code but at least it should give you an idea on how to solve your problem. You might have to make minor changes (the last line maybe replace it for a for loop if it doesn't work, etc)

Android adapter.add() crashing my application

in android application, i'm filling my spinner with some data coming from EditText object.
And when i'm trying to add it with adapter.add(somestring) method it crashes, so i need help.
...here's the code
public class OptionsMenu extends Activity implements View.OnClickListener{
Spinner users;
EditText input;
Button add,remove;
public static String filename = "savedData";
SharedPreferences sharedData;
String stringUsers;
ArrayAdapter<CharSequence> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options_menu);
Create();
sharedData = getSharedPreferences(filename, 0);
}
private void Create() {
// TODO Auto-generated method stub
users = (Spinner) findViewById(R.id.sp_op_users);
input = (EditText) findViewById(R.id.tb_op_inputUsers);
add = (Button) findViewById(R.id.bt_op_add);
remove = (Button) findViewById(R.id.bt_op_remove);
add.setOnClickListener(this);
remove.setOnClickListener(this);
//------------ADAPTER-----------------
adapter = ArrayAdapter.createFromResource(this, R.array.users,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
users.setAdapter(adapter);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.bt_op_add:
if (!input.getText().toString().equals("")) {
CharSequence inputData = input.getText().toString();
adapter.add(inputData);
adapter.notifyDataSetChanged();
users.setAdapter(adapter);
}
input.setText("");
users.setSelection(Adapter.NO_SELECTION);
break;
case R.id.bt_op_remove:
break;
}
}
Not very sure why you are getting the error but your if condition is wrong.
Change the following line:
if (input.getText().toString() != "")
to
if (!input.getText().toString().equals(""))
You don't compare strings using a = sign.
EDIT
Maybe you could first get the array from the resource file and create a local version of it:
String[] usersList=getResources().getStringArray(R.array.users);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, usersList)
and use this usersList as the list of data for your adapter.
You created the adapter using createFromResource() and provided it with the data from your resource. If you do it this way the list is fixed and you cannot add to or remove elements from it. This is why it crashed when you try to call adapter.add().
If you want to have the spinner contain dynamic data, then you'll have to add all the elements to it using add() and not create it from a resource.
EDIT: Add code example
in onCreate() we create and initialize the spinner adapter
List<String> items = ... // These are your items you get from a resource or read
// from a file or whatever
// Create the adapter, initializing it with the list of items, attach to spinner
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
for (String item : items) {
adapter.add(item);
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
users.setAdapter(adapter);
in onClick(), to add an item to the spinner:
case R.id.bt_op_add:
if (!input.getText().toString().equals("")) {
CharSequence inputData = input.getText().toString();
adapter.add(inputData);
// You shouldn't need to reset the adapter on the spinner, nor call
// notifyDataSetChanged() here
}
input.setText("");
users.setSelection(Adapter.NO_SELECTION);
break;

Categories