Why am I getting an java.lang.runtimeexception: Unable to instantiate activityComponentInfo {com.example.carprofile/com.example.carprofile.CarRegistraion}: java.lang.NullPointerExceprion
This is my code:
public class CarRegistration extends Activity{
//ParseXML parseClass;
Button saveCar;
EditText carMake = (EditText) findViewById(R.id.etCarMake);
EditText carModel = (EditText) findViewById(R.id.etCarModel);
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}
if I clear the onCreate method, from saveCar = (Button)..., everything works fine.
Can someone tell me what am I doing wrong?
Try this,
public class CarRegistration extends Activity{
Button saveCar;
String newCar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.car_registration);
EditText carMake = (EditText) findViewById(R.id.etCarMake); //these lines should be after setContentView()
EditText carModel = (EditText) findViewById(R.id.etCarModel);
saveCar = (Button) findViewById(R.id.bSaveCar);
saveCar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
newCar = carMake.getText() +" "+ carModel.getText();
//parseClass = new ParseXML(newCar);
//parseClass.editXML();
}
});
}
Related
I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The code throws compile time error:
Class 'Anonymous class derived from OnLongClickLister' is not abstract and does not override abstract method onLongClick(View) in OnLongClickListener
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean OnLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);
You're not overriding the right method. It's onLongClick, not OnLongClick. See below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amitsbutton = (Button) findViewById(R.id.amitsbutton);
amitsbutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("Small click is working");
}
}
);
amitsbutton.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
TextView amitstext = (TextView) findViewById(R.id.amitstext);
amitstext.setText("long click is also working ");
return true;
}
}
);
package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
i am getting error at
if(et.equals("ab123"))
'equals' is shown as error with cannot resolve symbol type
i have tried comparing some other predeclared string instead of 'et' but still it is generating same error
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
}
private void init() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
Write the following inside your OnCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("sss"))
{
System.out.println("asdfg");
}
}
Use the comparison code inside the method scope of onCreate(). In java, it is not allowed to do operations outside a method or an instance block or a static block inside a class.
Modify your own code like:
package com.example.life.bluebell;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
Log.d("Myactivity","asdfg");
//system out will not be of much use and will only print on console. Use log instead to see output in Logcat
}
}
}
Try this , I am sure this will work
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
try to put this code in some method like
public void getData(){
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123"))
{
System.out.println("asdfg");
}
}
and dont call this getData() in onCreate(),
call it in some Button click action like follows
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
your complete code would be like
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button cButton = (Button) findViewById(R.id.custom_button);
cButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getData();
}
});
}
public void getData() {
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString();
if(et.equals("ab123")) {
System.out.println("asdfg");
}
}
}
First u have to check the reference editText is correct or not, then after just follow the below codes,
public class login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkValue();
}
public void checkValue()
{
EditText edittext = (EditText) findViewById(R.id.editText);
String et = edittext.getText().toString().trim();
if(et.contentEquals("ab123")) // if(et.equalsIgnoreCase("ab123"))
{
System.out.println("asdfg");
}
}
}
It will help u.
I'm trying to use multiple data on a single listview through different buttons, as from activity1 there are two buttons, if button1 is clicked on activity2 it shows different data and if button2 is clicked then diff data respectively
//main
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class) );
}
});
}
}
//for second activity
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
if (R.id.button1 == clicked) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}
You should use putExtra() method of the Intent class. Put boolean extra in your MainActivity with key acceptors:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", true);
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, NEXT.class).putExtra("acceptors", false));
}
});
}
}
Then in your NEXT activity, get intent, and check whether you get acceptors or donors:
public class NEXT extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
boolean isAcceptors = getIntent().getBooleanExtra("acceptors", false);
if (isAcceptors) //did onClick="clicked"
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.acceptors)));
}
else
{
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.donars)));
}
}
}
I am a beginner, I'm trying to build a calculator.
I have a problem.
I write the answer like this:
ans += Double.parseDouble(etResult.getText().toString());
outTest.setText("The answer is " + ans);
This is in an OnClickListenter.
The problem is that I have to communicate with the double ans, which I defined in the beginning of my project, if I want to communicate with the double I need him to be final, but if he is final I cant change him in the OnClickListener.
Can anyone help me and tell me how can I do this? thanks.
Use the usual trick: instead of double ans = 0 declare final double[] ans = {0} and then you'll be able to use ans[0] += ... inside the handler code.
You can writ your code as follows
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Double dbl;
Button btn;
EditText et;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dbl = Double.parseDouble(et.getText().toString());
Log.e("double", "" + dbl);
}
});
}
}
Try it
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Double dbl;
Button btn;
EditText et1, et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dbl = Double.parseDouble(et1.getText().toString());
et2.setText(dbl.toString());
}
});
}
}