Why the symbol 'add' cannot be resolved? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to make an array list, but the add method is not working.
package com.zaination.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ListView VarListView = (ListView) findViewById(R.id.ListView);
ArrayList<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}

If you would put that code into onCreate (or any other) method it would probably work.

it's need to be updated like this.
package com.zaination.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView VarListView = (ListView) findViewById(R.id.ListView);
ArrayList<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}
}
also another hint attr name VarArrayList not follow java naming convention, should start with small letter varArrayList

You wrote the code outside of any method. It has to be in any kind of method.
The easiest way is to write it directly into the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView VarListView = (ListView) findViewById(R.id.ListView);
ArrayList<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}
PS:
There are some small improvements (also partly recommended by Android Studio) you could do:
you can ommit the cast (ListView) because it's redundant
you can ommit the ArrayList identifier string on initialization
you should name your objects (like VarArrayList) after the lower camel case naming convention (varArrayList):

Related

Android app crashing when trying to setText() from an arraylist<String> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I am a bit new to Android Studio and Java. I am trying to update a text by replacing it from an element of an ArrayList. However, when using setText(userList.get(0)), the app crashes. My guess is that the arraylist is empty or cannot be accessed. But I use the same code on a button with a setOnClickListener, the text is updated by the string found in the arrayList.
I want to make the app automatically update the Text onloading the activity.
Here is my MainActivity.java -- I commented the setText that is crashing the app.
package com.example.sampletest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> userList = new ArrayList<String>();
TextView text;
Button swapText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getUsers().execute();
text = (TextView) findViewById(R.id.text);
swapText = (Button) findViewById(R.id.button);
//text.setText(userList.get(0));
swapText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setText(userList.get(0));
}
});
}
class getUsers extends AsyncTask<Void, Void, Void> {
String error = "";
#Override
protected Void doInBackground(Void... voids) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.100.5:3306/cardetailingdb", "lau", "lau");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
while (resultSet.next()) {
userList.add(resultSet.getString(1));
}
} catch (Exception e) {
error = e.toString();
}
return null;
}
}
}
My guess is that the arraylist is empty or cannot be accessed.
The list is empty. You can see the stacktrace in the logcat for crash details.
But I use the same code on a button with a setOnClickListener, the text is updated by the string found in the arrayList.
That's because it's is not executed syncronously in onCreate() but later on clicking the button.
I want to make the app automatically update the Text onloading the activity.
You can add an onPostExecute() to your async task that gets run on the main thread once the background call completes. You should also check that the async call actually succeeded and not blindly trust that userList has elements in it.
In type of async task you should ensured first your all data is populated. Soo make sure background is completed and List is not empty. Hope will be work fine. For crashing issues you can add a check point like:
if(userList.size()>0){ text.setText(userList.get(0)); }

App crashes when editing view content in android - Java [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
whenever i try to edit the content of a list view or and text view through the java code my app just crashes and stops working
when i comment the line of code which edit the content the just works fine
in the following example when comment the line
lv1.setAdapter(AA1);
the app works
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView lv1;
ArrayAdapter<String> AA1;
ArrayList<String> names;
#Override
protected void onCreate(Bundle savedInstanceState) {
lv1= findViewById(R.id.lv1);
names = new ArrayList<String>();
names.add("Jake");
names.add("Amy");
names.add("Diaz");
names.add("Boyl");
AA1 = new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,names);
lv1.setAdapter(AA1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
you have to call those first
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
so your code will be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1= findViewById(R.id.lv1);
names = new ArrayList<String>();
names.add("Jake");
names.add("Amy");
names.add("Diaz");
names.add("Boyl");
AA1 = new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,names);
lv1.setAdapter(AA1);
}

Where is the id of a string array stored or where can i create one, IN ANDROID Programming?

I am learning Android programming from this tutorial about ArrayAdapters and ListViews.
The output does not show the string array values but instead shows: Item1, subitem1,, Item2,subitemm2...etc.
I want to know why did it happen? Do the values of string array need to be created separately or does Eclipse do that automatically? If so, where would I put/find the id mobile_list?
Here is the main activity class where string array declared:
package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListDisplay extends Activity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
Please refer to this question. You need to create your own class that extends from ArrayAdapter and override getView() so you can inflate your views accordingly and getCount() so the list view knows how many items are drawn. You are not supposed to use the default implementation of the ArrayAdapter. The default implementation and the way you are using it is taking as second parameter a TextView reference and you appear to be passing a layout. Refer to the official docs for this. That's why you need to create your own class
Change
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
To
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);

Unfortunately, [APP] has stopped! WHAT am i doing wrong? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is my first post at StackExchange.
I am a very beginner Android developer. I was making an app for saving arrays(lists). But unfortunately when I run my app it shows {Unfortunately, Shop has stopped!}. I am pretty much familiar with this error and many times I have also been able to get through it. But this time, I can't figure out what am I doing wrong. My code looks perfect but somehow keeps showing that error.
I am pasting my java code below. Please go through my code and point my mistake! PLEASE~!
AddItem.java
package com.rcube.shop;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddItem extends Activity{
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();
ArrayList<String> itemarray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
Button addnewitem = (Button) findViewById(R.id.addnewitem);
final EditText getitemname = (EditText)findViewById(R.id.getitem);
addnewitem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String newitemname = getitemname.getText().toString();
if(newitemname.length()!=0){
itemarray.add(newitemname);
Set<String> itemset = new HashSet<String>(itemarray);
edit.putStringSet("items_set", itemset);
edit.commit();
}else{
Toast.makeText(AddItem.this, "Write Item name before submitting!", Toast.LENGTH_LONG).show();
}
}
});
}
}
Please tell me what am I doing wrong. And I have add new android activity in Android Manifest also, so this error is not because of that.
Thanks again!
PEACE~!
Seems like you are getting NPE at getApplicationContect()
Put this in onCreate after setContentView
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();
You won't get context before that.
Hope this helps.
Put these 2 lines inside onCreate() after findViewById, then you can use context.
SharedPreferences store = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor edit = store.edit();

android - listview and Adapter does not show any item:(

I have a problem in my Android application. I have a Custom ListView Adapter, but when I launch the application the list does not show any item!
My Code :
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class AccountContents extends Activity {
private ListView ls1;
String username = fourshared.username;
String password = fourshared.password;
private AccountItem[] rootContents;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootContents = fourshared.rootContents;
CArrayAdapter adapter = new CArrayAdapter(AccountContents.this, rootContents);
setContentView(R.layout.main);
ls1 = new ListView(AccountContents.this);
ls1.setAdapter(adapter);
}
}
Does your layout contain a list view? If so, you should look that up and set the adapter on that:
ls1 = (ListView)findViewById(R.id.your_list_view_id);
Better still, make your activity a ListActivity and Android will do a lot of the work for you.
You need to add the list to your activity. To do this, add this line:
setContentView(R.layout.main);
and remove: setContentView(R.layout.main);
This will add the list into view, but remove the existing views.
or you can define a list in your xml, and, as mentioned above, find it like this:
ls1 = (ListView)findViewById(R.id.your_list_view_id);
and in xml:
<ListView android:id="#+id/ls1" ......allOtherAtributesHere...... />

Categories