This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am new to coding and enthusiastically learning Android Studio, following on videos to do coding.
I have already gone through that article - What is NullPointerException and understood the concept in few more related articles but I am unable to fix though I understand the error is on which code but if not that, then what is my question.
I am facing this NullPointerException after pressing any button on calculator app:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.blogeomics.calcee.MainActivity.numberPressed(MainActivity.java:167)
at com.blogeomics.calcee.MainActivity$7.onClick(MainActivity.java:97)
Code is below:
package com.blogeomics.calcee;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import static android.R.string.no;
import static android.os.Build.VERSION_CODES.O;
public class MainActivity extends AppCompatActivity {
String runningNumber = "0";
TextView resultsView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button oneBtn = (Button) findViewById(R.id.button1);
Button twoBtn = (Button) findViewById(R.id.button2);
Button threeBtn = (Button) findViewById(R.id.button3);
Button fourBtn = (Button) findViewById(R.id.button4);
Button fiveBtn = (Button) findViewById(R.id.button5);
Button sixBtn = (Button) findViewById(R.id.button6);
Button sevenBtn = (Button) findViewById(R.id.button7);
Button eightBtn = (Button) findViewById(R.id.button8);
final Button nineBtn = (Button) findViewById(R.id.button9);
Button zeroBtn = (Button) findViewById(R.id.button0);
ImageButton calcBtn = (ImageButton) findViewById(R.id.buttonEqual);
ImageButton divideBtn = (ImageButton) findViewById(R.id.buttonDivide);
ImageButton multiplyBtn = (ImageButton) findViewById(R.id.buttonMultiply);
ImageButton addBtn = (ImageButton) findViewById(R.id.buttonAdd);
ImageButton subtractBtn = (ImageButton) findViewById(R.id.buttonSubtract);
Button clearBtn = (Button) findViewById(R.id.buttonClear);
TextView resultsView = (TextView) findViewById(R.id.result_view);
resultsView.setText("");
oneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(1);
}
});
twoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(2);
}
});
threeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(3);
}
});
fourBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(4);
}
});
fiveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(5);
}
});
sixBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(6);
}
});
sevenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(7);
}
});
eightBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(8);
}
});
nineBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(9);
}
});
zeroBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberPressed(0);
}
});
calcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
subtractBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
multiplyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
divideBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
clearBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
void numberPressed(int number) {
runningNumber = runningNumber + String.valueOf(number);
resultsView.setText(runningNumber);
}
}
I have gone through lot of posts about this error but still unable to figure out how to SOLVE my problem, I know it should be a minor thing yet I can't comprehend, please help.
Change this line:
TextView resultsView = (TextView) findViewById(R.id.result_view);
to
resultsView = (TextView) findViewById(R.id.result_view);
now your resultsView will be global
Related
I have problem with my android app. It's soundbar application.
After run, everything works perfectly. After some time and clicking on buttons, I have error:
E/MediaPlayer: start called in state 0, mPlayer(0x7d176f4300)
E/MediaPlayer: error (-38, 0)
I run this on my Samsung S7, I would ask for understanding becouse I am beginner. I saw similar topics but I can't find answer which helps me. Here is my code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer AaaCoJeszcze = MediaPlayer.create(this, R.raw.aaa_co_jeszcze);
Button But1 = this.findViewById(R.id.Button1);
But1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AaaCoJeszcze.start();
}
});
final MediaPlayer CoNieMoznaMP = MediaPlayer.create(this, R.raw.co_nie_mozna);
Button But2 = this.findViewById(R.id.Button2);
But2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoNieMoznaMP.start();
}
});
final MediaPlayer CoToJaWinnyMP = MediaPlayer.create(this, R.raw.co_to_ja_winny);
Button But3 = this.findViewById(R.id.Button3);
But3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CoToJaWinnyMP.start();
}
});
final MediaPlayer debilMP = MediaPlayer.create(this, R.raw.debil);
Button But4 = this.findViewById(R.id.Button4);
But4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
debilMP.start();
}
});
final MediaPlayer iTenMP = MediaPlayer.create(this, R.raw.i_ten_o_co_chodzi);
Button But5 = this.findViewById(R.id.Button5);
But5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
iTenMP.start();
}
});
final MediaPlayer JedziemyMP = MediaPlayer.create(this, R.raw.jedziemy_z_kurwami);
Button But6 = this.findViewById(R.id.Button6);
But6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JedziemyMP.start();
}
});
final MediaPlayer JegoMP = MediaPlayer.create(this, R.raw.jego_robota_moze_poczekac);
Button But7 = this.findViewById(R.id.Button7);
But7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
JegoMP.start();
}
});
final MediaPlayer ZanimMP = MediaPlayer.create(this, R.raw.zanim_podeszlem);
Button But8 = this.findViewById(R.id.Button8);
But8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ZanimMP.start();
}
});
final MediaPlayer MozePanOdejscMP = MediaPlayer.create(this, R.raw.moze_pan_odejsc);
Button But9 = this.findViewById(R.id.Button9);
But9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozePanOdejscMP.start();
}
});
final MediaPlayer MozeZrobilPomylkeMP = MediaPlayer.create(this, R.raw.moze_zrobil_pomylke);
Button But10 = this.findViewById(R.id.Button10);
But10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeZrobilPomylkeMP.start();
}
});
final MediaPlayer MowiszTakTakMP = MediaPlayer.create(this, R.raw.mowisz_tak_tak);
Button But11 = this.findViewById(R.id.Button11);
But11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MowiszTakTakMP.start();
}
});
final MediaPlayer PaniMnieMP = MediaPlayer.create(this, R.raw.pani_mnie_rozprasza);
Button But12 = this.findViewById(R.id.Button12);
But12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PaniMnieMP.start();
}
});
final MediaPlayer PrawdopodobnieMP = MediaPlayer.create(this, R.raw.prawdopodobnie_polak);
Button But13 = this.findViewById(R.id.Button13);
But13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrawdopodobnieMP.start();
}
});
final MediaPlayer ProszeNiePrzeszkadzacMP = MediaPlayer.create(this, R.raw.prosze_nie_przeszkadzac_dobrze);
Button But14 = this.findViewById(R.id.Button14);
But14.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProszeNiePrzeszkadzacMP.start();
}
});
final MediaPlayer PrzepraszamPaniaMP = MediaPlayer.create(this, R.raw.przepraszam_pania_spoczko);
Button But15 = this.findViewById(R.id.Button15);
But15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PrzepraszamPaniaMP.start();
}
});
final MediaPlayer RozpraszaMP = MediaPlayer.create(this, R.raw.rozprasza_mnie);
Button But16 = this.findViewById(R.id.Button16);
But16.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozpraszaMP.start();
}
});
final MediaPlayer RozumiszMP = MediaPlayer.create(this, R.raw.rozumisz);
Button But17 = this.findViewById(R.id.Button17);
But17.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RozumiszMP.start();
}
});
final MediaPlayer spoczkoMP = MediaPlayer.create(this, R.raw.spoczko);
Button But18 = this.findViewById(R.id.Button18);
But18.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spoczkoMP.start();
}
});
final MediaPlayer SzefiemozeszMP = MediaPlayer.create(this, R.raw.szefie_mozesz_odejsc);
Button But19 = this.findViewById(R.id.Button19);
But19.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SzefiemozeszMP.start();
}
});
final MediaPlayer AleodejdzMP = MediaPlayer.create(this, R.raw.szefie_odejdz_ale_odejdz);
Button But20 = this.findViewById(R.id.Button20);
But20.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AleodejdzMP.start();
}
});
final MediaPlayer MozeMP = MediaPlayer.create(this, R.raw.moze_moze_moze);
Button But21 = this.findViewById(R.id.Button21);
But21.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MozeMP.start();
}
});
final MediaPlayer RekoczynyMP = MediaPlayer.create(this, R.raw.zadnych_rekoczynow);
Button But22 = this.findViewById(R.id.Button22);
But22.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RekoczynyMP.start();
}
});
final MediaPlayer PowinnoDojscMP = MediaPlayer.create(this, R.raw.juz_powinno_dojsc);
Button But23 = this.findViewById(R.id.Button23);
But23.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PowinnoDojscMP.start();
}
});
}}
I'm trying to implement onClick function for a Button on a custom Dialog but nothing happen when i click on the Button .it's the butEditAdd
MainActivity.java :
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AddDialog addDialog = new AddDialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
addDialog.java:
public class AddDialog extends Dialog implements android.view.View.OnClickListener{
Activity a;
Dialog d;
Button add , cancel;
RadioButton owes,lent ,money,things ;
EditText name ,amount,object,note;
Spinner spin;
public AddDialog(Activity c) {
super(c);
this.a = c;
}
#Override
public void onClick(View v){
};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogedit);
add=(Button)findViewById(R.id.buttonAdd);
cancel=(Button)findViewById(R.id.buttonCancel);
owes = (RadioButton)findViewById(R.id.radioButtonOwes);
lent = (RadioButton)findViewById(R.id.radioButtonLent);
money = (RadioButton)findViewById(R.id.radioButtonAmount);
things =(RadioButton)findViewById(R.id.radioButtonThings);
name = (EditText)findViewById(R.id.editName);
object = (EditText)findViewById(R.id.editTextWhat);
amount =(EditText)findViewById(R.id.editTextAmount);
note =(EditText)findViewById(R.id.editTextNote);
spin=(Spinner) findViewById(R.id.spinnerdevise);
owes.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
lent.setChecked(false);
}
});
lent.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
owes.setChecked(false);
}
});
money.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.setChecked(false);
object.setEnabled(false);
amount.setEnabled(true);
spin.setEnabled(true);
object.setText("");
}
});
things.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.setChecked(false);
object.setEnabled(true);
amount.setEnabled(false);
spin.setEnabled(false);
amount.setText("");
}
});
object.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
things.performClick();
things.setChecked(true);
}
});
amount.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
money.performClick();
money.setChecked(true);
}
});
thanks for help!
Dialog fragment has separate set of life cycles.If you want to create a class adddialog,then extend dialog fragment and in oncreate of adddialog use setcontent view.
If you want to create simple dialog try this line of code.This will solve your problem.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.btn);
//final Button butEditAdd=addDialog.getAdd();
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working", Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
Change AddDialog to Dialog
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Dialog addDialog = new Dialog(MainActivity.this);
addDialog.setContentView(R.layout.dialogedit);
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
addDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//addDialog.dismiss();
Toast.makeText(getApplicationContext(),"It's working",Toast.LENGTH_SHORT).show();
}
});
addDialog.show();
}
});
you need it like this
final Button butEditAdd =(Button)addDialog.findViewById(R.id.buttonAdd);
You should inflate the layout first, then find the view by its id.
LayoutInflater inflater = LayoutInflater.from(this);
View dialogView = inflater.inflate(R.layout.dialogedit, null);
final Button butEditAdd =(Button) dialogView.findViewById(R.id.buttonAdd);
.............
butEditAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do something
}
});
This should work.
use this instead of getApplicationContext().
Remove the setContentView(R.layout.dialogedit) from your dialog onCreate().
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And your rest of the code working fine.
I am new to Java and android programming.Can someone explain to me what is wrong here?
When i run it it gives me the errors:
Error:illegal start of expression, Error: identifier expected, Error:
not a statement, Error: ';' expected, Error: ')' expected,
Error:illegal start of type, Error:reached end of file while parsing.
Here is the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
cob.getText().toString();
nop.getText().toString();
public void total = cob+ nop;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(final.total)
})
//TextView
TextView tv = (TextView) findViewById(R.id.textView);
}
}}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(final.total)
})
Should be immediately followed by an ';', same goes for the setText call.
An error like that always means you forgot one of those, or misplaced/forgotten some brackets, ...
Since you don't state the exact error message: I'm just guessing it's this error, but it's possible there are more in your code.
EDIT:
And, as others already mentioned: public void total = cob+ nop; makes no sense at all. void is not a valid datatype in Java, so you can't declare a 'void'.
The second button click event is not right.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(final.total);
}
});
total is also wrong.
It should be
int total = Integer.parseInt(cob.getText().toString()) + Integer.parseInt(nob.getText().toString()) ;
Putting all these together
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
TextView tv = (TextView) findViewById(R.id.textView);
int a = Integer.parseInt(cob.getText().toString());
int b = Integer.parseInt(nop.getText().toString());
public void total = a + b;
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(total);
}
});
}
int total = Integer.parseInt(cob.getText().toString())+ Integer.parseInt( nop.getText().toString());
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(String.valueOf(total));
});
remove
cob.getText().toString();
nop.getText().toString();
public void total = cob+ nop;
My problem is in set OnClickListener I don't know why but always at the start my game crushes:
ImageButton button1 = (ImageButton) findViewById(R.id.imagePlay);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
callListOfLevels();
}
});
ImageButton button2 = (ImageButton) findViewById(R.id.imageQuit);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void callListOfLevels(){
Intent intent = new Intent(this, ListOfLevels.class);
startActivity(intent);
}
Hello i'm getting the error:
the method setonclickListener(new onclickListener(){}) is undefined
for the type button
Here's my code:
public class Sliding extends Activity {
/** Called when the activity is first created. */
private LinearLayout MenuList;
private Button btnToggleMenuList;
private int screenWidth;
private boolean isExpanded;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding);
MenuList = (LinearLayout) findViewById(R.id.linearLayout2);
btnToggleMenuList = (Button) findViewById(R.id.button1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
if (isExpanded) {
isExpanded = false;
MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}else {
isExpanded = true;
MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20));
}
}
});
}
}
can anyone tells me why?
Replace this
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
By
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Also make sure you have the below import statement
import android.view.View.OnClickListener;
Change it to
View.OnClickListener()
Write this:
btnToggleMenuList.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rather than:
btnToggleMenuList.setonclickListener(new onclickListener() {
public void onclick(View v) {
Try this:
btnToggleMenuList.setonclickListener(new onclickListener() {
#Override
public void onclick(View v) {