I want to send a string array list from my MainActivity to Creating activity via using intent. but it gives me some error
I am dealing with it for some time-_-.
This arraylist is for adding some words. I tried putextra(); and putStringArrayListExtra();
MainActivity
Intent intent = new Intent(getApplicationContext(),creating.class);
intent.putStringArrayListExtra("w",Words);
intent.putStringArrayListExtra("m", Meanings);
startActivityForResult(intent,REQUEST_CODE);
CreatingActivity
private ArrayList<String> Words1;
private ArrayList<String> Meanings1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creating);
Words1 = new ArrayList<String>();
Meanings1 = new ArrayList<String>();
Words1 = intent1.getStringArrayListExtra("w");
Meanings1 = intent1.getStringArrayListExtra("m");
when I click the button to come to this activity, this error appears:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.content.Intent.getStringArrayListExtra(java.lang.String)' on a null object reference
I thing You are missing something it should be like:
Words1 = getIntent().getStringArrayListExtra("w");
Meanings1 = getIntent().getStringArrayListExtra("m");
Right?
And if still you are facing issue try to debug because i think your way is correct.
Related
I am new in Android. I need your help. My Problem is - I have 2 classes Nplist.java and Addcustomer.java. In my AddCustomer class,One TextView and one Button is there to go Nplist class. In my Nplist class there is checklist and this checklist is coming from database and all the checked values are stored in ArrayList<String> and one Button is used to go back to AddCustomer class. I want that ArrayList<String> which is in Nplist to by display in my AddCustomer class Textview . I haved tried these but my Addcustomer class crashed.
1.Nplist.class
add.setOnClickListener(new View.OnClickListener() {<br>
#Override<br>
public void onClick(View view) {<br>
Bundle extra=new Bundle();<br>
extra.putSerializable("objects",checkedList);<br>
Intent intent = new Intent(Nplist.this, AddCustomer.class);<br>
intent.putExtra("extra",extra);<br>
startActivity(intent);<br>
});
2.AddCustomer.class
onCrete()...{
Bundle extra = getIntent().getBundleExtra("extra");<br>
ArrayList<String> object = (ArrayList<String>)extra.getSerializable("objects");<br>
for (String str : object) {<br>
getnp.append(str + "\n");<br>
}
}
What do you expect the result to be?
- What is the actual result you get? (Please include any errors.)
When i go like this Nplist-->AddCustomer its working well but crash on ( AddCustomer-->Nplist-->AddCustomer)
It's because when you are coming back to your AddCustomer Activity the list is null . You can solve this problem by making a global class which will store the list in a Static Field , And You can access that list from any class or Activity you want to. Try out below solution .
Global.java Class is as below :
public class Global {
private static ArrayList<String> object = new ArrayList<>();
public static ArrayList<String> getObject() {
return object;
}
public static void setObject(ArrayList<String> object) {
Global.object = object;
}
}
From NpList.java class set the value of the list as below :
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Global.setObject(checkedList);
Intent intent = new Intent(Nplist.this, AddCustomer.class);
startActivity(intent);
}
});
Now Access in AdCustomer.java as below :
onCreate()...{
Bundle extra = getIntent().getBundleExtra("extra");
ArrayList<String> object = Global.getObject();
for (String str : object) {
getnp.append(str + "\n");
}
}
This maybe helpful for you.
Using putStringArrayListExtra method you can send an array list of strings along with the intent.
Sender side:
Intent intent = ...
intent.putStringArrayListExtra("THE_KEY", theList);
Receiver side:
ArrayList<String> theList = intent.getStringArrayListExtra("THE_KEY");
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
So I have an activity in which it gets an XML file through a web service. It reads the XML and stores the relevant texts from that XML to a List<String> object called xmlText. After retreiving all the text, I am starting another activity in which it contains a Linear layout, inside the linear layout is a ListView. I am passing the contents of xmlText by populating an ArrayList<String> object called xmlInString. I then pass xmlInString through an intent.
I want to populate the listView to display the text from the xml file. In the activity I just started, I am populating a List<String> object in which it will contain the elements from xmlInString. I then instantiate the arrayadapter in which the listview calls the method, setAdapter() and I pass in the arrayadapter object.
But everytime I run it, i get an null pointer exception when I run the appCaused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.w1481217.organiser.wordXMLList.populateList(wordXMLList.java:43)
This is my code:
public class wordXMLList extends Activity {
Intent i;
ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> xmlInString;
List<String> xml;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wordlisting);
i = getIntent();
xmlInString = i.getStringArrayListExtra("xml");
lv = (ListView) findViewById(R.id.listView2);
populateList(xmlInString);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, xml);
lv.setAdapter(adapter);
}
private void populateList(ArrayList<String> xmlToConvert) {
for(int i =0; i < xmlToConvert.size(); i++){
xml.add(xmlToConvert.get(i));
Log.d("threading", "final output in xmlToConvert: " + xmlToConvert.get(i));
}
}
}
Any help will be appreciated.
Try this:
private void populateList(ArrayList<String> xmlToConvert) {
xml = new ArrayList<>();
for(int i =0; i < xmlToConvert.size(); i++){
xml.add(xmlToConvert.get(i));
Log.d("threading", "final output in xmlToConvert: " + xmlToConvert.get(i));
}
}
What I tried to do:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> voiceResults = getIntent().getExtras()
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
Card ShowDataCard = new Card(this);
ShowDataCard.setText(voiceResults);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
Failed work-around:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String voiceResults = getIntent().getExtras()
.getString(RecognizerIntent.EXTRA_RESULTS);
Card ShowDataCard = new Card(this);
ShowDataCard.setText(voiceResults);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
The String voiceResults is a failed workaround to the fact I cannot .setText to an arraylist string
Actual voice recognition code:
ArrayList<String> voiceResults = getIntent().getExtras()
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
Basically I am trying to save this voice recognition (going to use SharedPreferences) but for some reason it is an arraylist string and not just a string. Anybody know how to save it as a String? (or publish an activity with an arraylist string)
Since the results are an ArrayList of strings, you should check that the size() of the list is at least 1 and then call voiceResults.get(0) to get the string.
For my class we have to create an application where we catch an SMS message in a broadcast receiver, get the string (assumed to be a URL), add it dynamically to a string-array which is displayed in a fragmentlist. When the list item is clicked we then have to load it into a webview in a fragment.
Everything works until here.
The problem is that the list doesn't update when I try to add the url_act string to it.
Here's my code:
public class UpdateString extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url_act = "";
Bundle b = getIntent().getExtras();
url_act = b.getString("url");
UrlListFragment uf = (UrlListFragment) getFragmentManager()
.findFragmentById(R.id.listFragment);
String[] urls = getResources().getStringArray(R.array.urls_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(urls);
ArrayList<String> arrayList = new ArrayList<String>(list);
arrayList.add(url_act);
urls = arrayList.toArray(new String[list.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdateString.this,
android.R.layout.simple_list_item_1, urls);
uf.setListAdapter(adapter);
}
If you need more code for context let me know. Updated
You really should post your logcat output just to be sure, as otherwise we don't know what kind of exception was thrown.
That being said, I bet it is a NullPointerException; findFragmentById() is most likely returning null. This is probably because you never inflate your XML resource to begin with.
I'm a novice programmer and I'm trying to learn Android coding using Eclipse.
This is my first time using StackOverflow.
Just for tutorial purposes, I want to make a simple Animal Encyclopedia.
So in my Home class, there are some buttons: "Dog", "Cat", "Bird", etc. When I click the button, it will bring me to the same layout but of course with different content.
So I created a class named AnimalData that holds the
ArrayList<Integer> to store R.drawable.xxx and ArrayList<String>
to store the text that I will put below the picture (like "Bulldog"
or "Husky")
Then I created a class named ChangeContent to set all those drawable
and text to the XML
But whenever I click the button, it results in Stopped Unexpectedly Error
Below are the shortened Home class, The "crash-maker" isn't here. I have checked the whole code line per line using Thread.sleep(2000), so if my app crashes before 2 second, the error is before the sleep() code and vice versa.
public class Home extends Activity implements OnClickListener{
Button dog, cat, bird;
AnimalData ad;
ChangeContent cc;
private ArrayList<Integer> drawable;
private ArrayList<String> title;
public Home(){
ad = new AnimalData();
cc = new ChangeContent(ad);
drawable = new ArrayList<Integer>();
title = new ArrayList<String>()
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
//set the findViewById for all the buttons
//set onClickListener() to all the buttons
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bDog:
drawable.add(R.drawable.xxx);
drawable.add(R.drawable.yyy);
title.add("Bulldog");
title.add("Husky");
break;
case R.id.Cat:
//same
break;
case R.id.bBird:
//same
break;
}
ad.setDrawable(drawable);
ad.setTitle(title);
Intent i = new Intent("animal.ChangeContent"); //from Manifest
startActivity(i);
}
}
The AnimalData is just a typical getter setter, so I will just skip the code for that
The error is right after ChangeContent started because even when I put the sleep() on the first line of constructor, it doesn't have any effect.
public class ChangeContent extends Activity {
TextView title1, title2;
ImageView pic1, pic2;
private ArrayList<Integer> drawable;
private ArrayList<String> title;
public ChangeContent(AnimalData data){
drawable = new ArrayList<Integer>();
title = new ArrayList<String>();
drawable = data.getDrawable();
title = data.getTitle();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animal_info);
//findViewById for the TextView and ImageView
//setText() for TextView and setImageResource() for ImageView
}
}
Sorry for the long question, I tried to make it as short as possible
Can you guys help me figure the error out?
Thanks before
You are trying to get arraylist from intent, whereas you are not putting putStringArrayList and putIntegerArrayList methods.
putIntegerArrayListExtra(String name, ArrayList<Integer> value)
putStringArrayListExtra(String name, ArrayList<String> value)
so Change calling activity to following:
Intent i = new Intent(Home.this, ChangeContent.class); //from Manifest
i.putIntegerArrayListExtra("drawables", drawable);
i.putStringArrayListExtra("titles", titles);
startActivity(i);
and get data from intent in onCreate method by following methods:
getIntegerArrayListExtra, and getStringArrayListExtra
you also can do following by making contentChanged method to static, by this you wont need to do much changes in your application code, just do following:
public class ChangeContent extends Activity {
TextView title1, title2;
ImageView pic1, pic2;
private static ArrayList<Integer> drawable;
private static ArrayList<String> title;
public static ChangeContent(AnimalData data){
drawable = new ArrayList<Integer>();
title = new ArrayList<String>();
drawable = data.getDrawable();
title = data.getTitle();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animal_info);
//findViewById for the TextView and ImageView
//setText() for TextView and setImageResource() for ImageView
}
}
See the answer by RajaReddy P here:
https://stackoverflow.com/a/9937854
In the send class use:
Intent intent = new Intent(PhotoActivity.this,PhotoActivity1.class );
intent.putIntegerArrayListExtra("VALUES", image);
startActivity(cameraIntent);
.. and in the receiver class use:
Intent i = getIntent();
ArrayList<Integer> img = i.getIntegerArrayListExtra("VALUES");
Your approach is wrong -
public Home(){
ad = new AnimalData();
cc = new ChangeContent(ad);
drawable = new ArrayList<Integer>();
title = new ArrayList<String>()
}
ChangeContent is a Activity class so, you don't pass parameter like this.
Passsing class should be serializable and use it -
startActivity(new Intent(this, ChangeContent.class)
.putExtra("key", ad);
And in your ChangeContent class extract the class from Intent
So, change your code design and go ahead.
Best of luck.