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);
}
Related
Im just starting out with android studio and ive run into this block with an app that im trying to build.
Ive created a list view with the names of COVID vaccines, I want to code in such a way that when the user clicks on a vaccine a window/new activity pops up that shows them all the info about the vaccine in the form of a table.
How can I do that?
This is the Coding I have so far:
package com.example.covid_vaccine;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listViewVaccines = (ListView) findViewById(R.id.listView);
ArrayAdapter<CharSequence> adapterVaccines = ArrayAdapter.createFromResource(this,
R.array.vaccineArray,
android.R.layout.simple_list_item_1);
listViewVaccines.setAdapter(adapterVaccines);
}
}
You need to add OnItemClickListener in your adapter class
If you are new to Android studio see this tutorial from youtube to add OnItemClickListener it may help you
https://youtu.be/bhhs4bwYyhc
Happy coding:)
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):
This question already has answers here:
How to create a method inside onCreate() in Android
(2 answers)
Closed 4 years ago.
I have been following this tutorial to switch activites with a button - https://developer.android.com/training/basics/firstapp/starting-activity
I am getting the error "Cannot resolve symbol view" from searching this is usually because people haven't imported View, I have.
Also seems to think newPacket is a variable "Variable 'newPacket' is never used"
Can't for the life of me workout what is going wrong here
package com.alexitconsoluting.app.contactclock;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainClock extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_clock);
public void newPacket(View view) {
Intent intent = new Intent(this, NewPacket.class);
startActivity(intent);
}
}
}
You are creating a method inside method which is not allowed
Create Outside of OnCreate()
public class MainClock extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_clock);
}
public void newPacket(View view) {
Intent intent = new Intent(this, NewPacket.class);
startActivity(intent);
}
}
This question already has answers here:
Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
(9 answers)
Closed 5 years ago.
I wrote the code to receive name from EditText, but when I initialize EditText variable my app crashes
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class test_activity extends AppCompatActivity {
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_activity);
}
final EditText editText = (EditText) findViewById(R.id.editText);**
public void set_text(View view){
name = editText.getText().toString();
}
}
You need to initialize your views inside onCreate or after it is called. Move your line which is causing the error inside the onCreate method.
I am trying to finish off the first tutorial in the Android Studio documentation, but I can't even seem to do that.
I keep getting a:
Error:(17, 60) error: cannot find symbol variable EXTRA_MESSAGE
Here is the code to my DisplayMessageActivity.java file:
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
}
The tutorial I am trying to follow is here:
https://developer.android.com/training/basics/firstapp/starting-activity.html
There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = <Some Message String>. And this error will be resolved
There is no static variable with name EXTRA_MESSAGE. Add public static String EXTRA_MESSAGE = .