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.
Related
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);
}
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I'm trying to web scrape from a website https://www.worldometers.info/coronavirus/ and turns that data to form an app but the data is not actually printing I don't the reason but whenever I click the button in an android emulator it just crashes instantly !!
I have 3 textView in the app and a button so whenever I click the button it should show up the data in the textView !!
textView have ids of TotalCases / TotalDeaths / TotalRecovered
button has an id of the button
Here is what I have I have done
package com.example.coronaupdate;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Button btn;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
compare();
}
});
}
public void compare()
{
final TextView totalCases;
final TextView totalDeaths;
final TextView totalRecovered;
totalCases = (TextView) findViewById(R.id.TotalCases);
totalDeaths = (TextView) findViewById(R.id.TotalDeaths);
totalRecovered = (TextView) findViewById(R.id.TotalRecovered);
try {
Document doc = Jsoup.connect("https://www.worldometers.info/coronavirus/").userAgent("mozilla/17.0").get();
Elements temp = doc.select("div.maincounter-number");
Element totalcase = temp.get(0);
String cases = totalcase.select("div.maincounter-number span").text();
totalCases.setText(cases);
Element totaldeaths = temp.get(1);
String deaths = totaldeaths.select("div.maincounter-number span").text();
totalDeaths.setText(deaths);
Element totalrecovered = temp.get(2);
String recovered = totalrecovered.select("div.maincounter-number span").text();
totalRecovered.setText(recovered);
/* for(Element totalCase:temp)
{
String cases = totalCase.select("div.maincounter-number span").text();
System.out.println("" + cases);
*//*i++;
System.out.println(i + "" + totalCase.getElementsByTag("span"));*//*
}*/
}
catch (IOException e){
e.printStackTrace();
}
}
}
image of the app view
Image of the app view
If you could post the Error you're getting it would be easier to see what's going on, but I have two assumptions of what's wrong.
First you should check if yout Manifest is asking for INTERNET access permission. If it isn't, you should include it.
Second: Always when making requests to the internet in Android you should use either AsyncTasks or at least open a separate thread manually. That's because internet calls are asynchronous by definition, and if you block the main execution of your App to wait for a response the UI is gonna freeze or crash. So perhaps you should extract the logic of your compare() function to a separate class that inherits from AsyncTask and place it into the doInBackground() method.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
java.lang.NullPointerException- error in Android
(1 answer)
How to Handle NullPointerException in android?
(2 answers)
Closed 3 years ago.
I tried a number of things but my Application keeps Crashing.
I tried debugging it is returned this error when I run the application.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
package com.zybooks.pigdice;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final Context context = this;
//private EditText playerName;
private String name;
private int target_score_display;
//public TextView player_name;
private EditText targetScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//player_name = findViewById(R.id.player_name);
targetScore = dialog.findViewById(R.id.enter_target_score);
//playerName = findViewById(R.id.enter_player_name);
Button start_game = findViewById(R.id.start_game);
start_game.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialogbox);
dialog.setTitle("Title...");
Button dialogButton = dialog.findViewById(R.id.start_game_dialog);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
String value = targetScore.getText().toString();
target_score_display = Integer.parseInt(targetScore.getText().toString());
Intent intent = new Intent(getApplicationContext(),Gameplay.class);
intent.putExtra("TARGET_SCORE",target_score_display);
Bundle bundle = new Bundle();
bundle.putString("TARGET_SCORE",value);
intent.putExtras(bundle);
startActivity(intent);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}
});
dialog.show();
}
});
}
}
Trying to pass input taken in the dialog box to the next Activity. Have been struggling with this for over 3 hours.
You can only call dialog.findViewById after the dialog is created (inside the onClick). It has no content when the Activity is created, so you can't find its views. For example, dialogButton is gotten correctly
So when you try to get the text from the non existing view, it will throw the exception
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);
}
}
I'm new to Android Studio and Java, currently I'm learning about intents and sending data/text from one activity to another. On the second activity I get "Cannot resolve symbol 'intent'" even though it's in onCreate field. What I'm trying to do is to have two text fields for first and last name, sending the text from first activity to the second one which will read it, and all done with onButtonClick method. I'm only trying to do the first name and the code looks like this MainAcitivty
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClicked(View view) {
EditText nameText = (EditText)findViewById(R.id.firstName);
TextView messageText = (TextView) findViewById(R.id.messageTextView);
if(nameText.getText().toString().trim().length() == 0){
messageText.setText("text here");
} else {
String textForSecondAcitivity = "your name is: " + nameText.getText();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
startActivity(intent);
`
The problem is the second Activity that gives me the error "Cannot resolve symbol 'intent'". And here is the code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent scIntent = getIntent();
String messageText = intent.getStringExtra("EXTRA_TEXT");
TextView messageView = (TextView)findViewById(R.id.textView);
messageView.setText(messageText);
}
It wont show the error here but it's int this line
String messageText = intent.getStringExtra("EXTRA_TEXT");
I apologize in advance because surely I messed up something bad, I'm a beginner and if you have some advice on what's the best way to do what I'm actually trying to accomplish feel free to tell me.
The app should look like this in the end 1.The first activity
1.The second one showing first name and last name
You have no local variable named intent
Replace it with scIntent
Here you are making it wrong
intent.putExtra(Intent.EXTRA_TEXT, textForSecondAcitivity);
instead you use this
intent.putExtra("EXTRA_TEXT", textForSecondAcitivity);