Android: Unfortunately <app name> has stopped - java

I'm new to android development, this is just my second app i built just to kill time, though, i could'nt understand, what caused this error. Need expert advice
The activity_starting_point.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".StatingPoint" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 1"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Enter Number 2"
android:id="#+id/tvDisplay"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add"
android:layout_gravity="center" />
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Your Total is 0 "
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
</LinearLayout>
The Java class file: StartingPoint.java
package com.ankur.calulator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StatingPoint extends Activity {
int num1,num2;
int total;
Button add,sub;
TextView display;
EditText no1,no2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stating_point);
total=0;
no1=(EditText) findViewById(R.id.editText1);
no2=(EditText) findViewById(R.id.editText2);
display=(TextView)findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1+num2;
display.setText("Your Total is "+total);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
total=num1-num2;
display.setText("Your Total is "+total);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_stating_point, menu);
return true;
}
}
The logcat file:
its got blank, i think i messed up with it, though i saw something as java null pointer exception before
i knw i'm close, just nt able to find some missing link, need advice.

You haven't initialized add and sub like you do for no1,no1 and display
And you are trying to call setOnClickListener on add and sub without initializing so you get a NullPointerException
You need to provide different id to add and sub and initialize it.

In your activity_starting_point.xml, you have:
3 TextViews with the same id: tvDisplay
2 Buttons with the same id: button1
Please assign unique AND meaningful IDs to be able to reference them later in your java code, eg:
android:id="#+id/btAdd"
and
android:id="#+id/btSubtract"
(NOTE: you actually don't need id for TextViews with instructions, eg. all those tvDisplay, unless you want to actually reference them in Java, for example to change the instruction text)
Next, as spotted by Apoorv, whereas you initialize your no1 and no2, you don't do it with add and sub, hence they are null. Just initialise them the same way the same way as the above:
add = (Button) findViewById(R.id.btAdd);
and
sub = (Button) findViewById(R.id.btSubtract);
Also, in your OnClickListeners, your calculations will always yield 0 for total, because nowhere in the code you're actually getting values for num1 and num2. You probably want to do something like:
num1 = Integer.parseInt(no1.getText().toString());
num2 = Integer.parseInt(no2.getText().toString());

Related

Creating a Calculator Android app [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
I'm learning android programing and i just dont know how turn my xml into a working calculator.
here is my xml, i removed the styling for better reading
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.afm.calculator.MainActivity" >
<TextView
android:id="#+id/tvresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp" />
<Button
android:id="#+id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:textSize="40sp" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dp" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="รท"
android:textSize="40sp" />
<Button
android:id="#+id/mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40sp" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="40sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
here is my java:
package com.afm.cal;
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 implements android.view.View.OnClickListener {
Button mines, plus, divide, times, equal;
EditText eto, ett;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mines = (Button) findViewById(R.id.mines);
plus = (Button) findViewById(R.id.plus);
divide = (Button) findViewById(R.id.divide);
times = (Button) findViewById(R.id.times);
equal = (Button) findViewById(R.id.equal);
eto = (EditText) findViewById(R.id.editText1);
ett = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.tvresult);
}
public void onClick(View arg0) {
}
}
please help me finish it and thx in advance
First add Listener to all your buttons using setOnClickListener method.
Eg: plus.setOnClickListener(this);
Make modifications in your onClick method something like below.
public void onClick(View view) {
if (view == mines) {
// do the calculation here
//update the TextView using tv.setText()
} else if (view == plus) {
// do the calculation here
//update the TextView
}
//.. So on for all buttons
}
Assign each button a method using android:onClick attribute of Button in xml.
For example android:onClick="doSomething" . This button will call the doSomething() method when it is clicked.
Your methods should get values from TextFields and do the operation inside these methods.Then display the result in TextView

java.lang.RuntimeException: Unable to start activity and skipping frames

I am working on an android Temperature converter app(which does not work). At first I saw this message on LogCat
the application may be doing too much work on its main thread
I then removed the code that was never used.
Now, this is the error:
java.lang.RuntimeException: Unable to start activity
Code: Main_Activity.java
package com.example.tempconverter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editCelsius = (EditText) findViewById(R.id.editCelsius);
final EditText editFahrenheit = (EditText) findViewById(R.id.editFahrenheit);
Button buttonConvert =(Button)findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
double celsius = Double.valueOf(editCelsius.getText().toString());
double fahrenheit = (celsius * 9)/5 +32;
editFahrenheit.setText(String.valueOf(fahrenheit));
}
});
}
}
The code in fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tempconverter.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textCelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="55dp"
android:text="Celsius"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="hardcodedtext" />
<EditText
android:id="#+id/editCelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textCelsius"
android:layout_marginLeft="52dp"
android:layout_toRightOf="#+id/textCelsius"
android:ems="7"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textFahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editCelsius"
android:layout_marginTop="31dp"
android:text="Fahrenheit"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="hardcodedtext" />
<Button
android:id="#+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editFahrenheit"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="Convert"
tools:ignore="hardcodedtext" />
<EditText
android:id="#+id/editFahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editCelsius"
android:layout_alignTop="#+id/textFahrenheit"
android:ems="7"
android:inputType="number" />
</RelativeLayout>
this is a part of the LogCat now ...
https://drive.google.com/file/d/0Bxd5Rg3QFBfbdkZULWxNUTVrV2M/edit?usp=sharing
Apparently, you are inflating the wrong view. You've got a RuntimeException, and you should have a NullPointerException, because the setContentView try to set (and methods findViewById try to find views inside) the wrong layout as:
setContentView(R.layout.activity_main);
whereas all your EditTexts, TextViews and Buttons are in fragment_main.xml.
Try to set the content view as follow:
setContentView(R.layout.fragment_main);
or, rename fragment_main.xml to activity_main.xml.
Update from comments:
These kinds of errors "Couldn't load memtrack module' occur with some devices with emulator. To avoid this, always test with multiple devices (real or not).

MainActivity.java all grayed out

I added splash.xml and did project - clean which led to losing R.java. The cause was I have an mp3 file in raw folder which was giving error. I deleted that mp3 file and got back the R.java. Now my MainActivity.java is all grayed out. It's like none of the import or the code linked to activity_main.xml. All looks like something I have typed on notepad. Can someone point out what's wrong here?
package com.tenz.secondApp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Yout total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Subtract one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bSub" />
</LinearLayout>
I think you have multiple packages in your application.Please see your gen folder to verify that R.java in which package.and then
import packagename.R
You need to correct your imports.
Just import R.java from your project
import <YourPkgName>.R;
You have not closed LinearLayout at the beginning properly hence the error. Use the below code. I tested by removing that error, it works perfectly fine..check this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:onClick="Add one"
android:text="Subtract one"
android:layout_gravity="center"
android:textSize="20sp"
android:id="#+id/bSub" />
</LinearLayout>

I cannot add functionality to the next button in my app. It should change the text every time it's pressed.

I cannot add functionality to the next button in my app. It should change the text every time it's pressed. Though, in my case, what it does is change the text for once and then goes to sleep! What should I do? I searched the website but couldn't find any way!
Java Code!
package com.dreamgoogle.gihf;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class Quotes extends Activity {
ImageButton next;
ImageButton previous;
ImageButton copytext;
TextView q;
TextView nm;
String[] str;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quotes);
varSet();
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = 0;
i = i + 1;
q.setText(str[i]);
}
});
}
private void varSet() {
next = (ImageButton) findViewById(R.id.next);
previous = (ImageButton) findViewById(R.id.previous);
copytext = (ImageButton) findViewById(R.id.copy);
q = (TextView) findViewById(R.id.quotes);
str = new String[] {
"In order to succeed, your desire for success should be greater than your fear of failure.",
"A successful man is one who can lay a firm foundation with the bricks others have thrown at him.",
"Success is not final, failure is not fatal: it is the courage to continue that counts.",
"Try not to become a man of success. Rather become a man of value.",
"Want Something? Go Get It!" };
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quotes, menu);
return true;
}
}
Layout File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Quotes" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/h1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/quotes"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:gravity="center_vertical|center_horizontal|top"
android:text="#string/h2"
android:textSize="12sp"
android:typeface="monospace" />
<ImageButton
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/quotes"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/textView1"
android:background="#android:color/transparent"
android:contentDescription="#drawable/previous"
android:src="#drawable/previous" />
<ImageButton
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/previous"
android:layout_toRightOf="#+id/textView1"
android:background="#android:color/transparent"
android:contentDescription="#drawable/next"
android:src="#drawable/next" />
<ImageButton
android:id="#+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/previous"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:contentDescription="#drawable/copy"
android:src="#drawable/copy" />
<TextView
android:id="#+id/nm"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_above="#+id/quotes"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:clickable="false"
android:ems="10"
android:text="#string/h3" />
</RelativeLayout>
Change:
#Override
public void onClick(View v) {
int i = 0;
i = i + 1;
q.setText(str[i]);
}
To
#Override
public void onClick(View v) {
i = i + 1;
q.setText(str[i]);
}
By declaring i again within the onClick() method, you hide the one in your Activity class. And since this method is called every time, i is always reset to 0, and hence your text will always be the value of str[1]
This is known as variable hiding.
You are declaring the iterator inside your onClick. What happens when you click is:
1) You create a new int with the value of 0;
2) 0 = 0 + 1;
3) you set q's text to str[1] each and every time.
Declare your iterator as a class variable for the OnClickListener or the whole Activity itself.

setOnClickListener causes force close in Android

I am trying to create an onClickListener for a button. Everything shows up fine before I create the event listener, but force closes when I add it in.
Here is the code:
package com.austin.mobile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TxtLingoActivity extends Activity {
/** Called when the activity is first created. */
Button convert;
EditText inputText, outputText;
String myCopy;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
convert = (Button) findViewById(R.id.inputTextBox);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the xml:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/inputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/convert"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Convert" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/outputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Log Cat is showing this error:
03-19 18:05:21.726: E/dalvikvm(662): Unable to open stack trace file'/data/anr/traces.txt': Permission denied
Any suggestions?
Looks like you are referring to the incorrect id when 'finding' the Button:
Try to change this:
convert = (Button) findViewById(R.id.inputTextBox);
to this:
convert = (Button) findViewById(R.id.convert);
Hope this helps...
its ClassCastException in this line convert = (Button) findViewById(R.id.inputTextBox);
Because R.id.inputTextBox is EditText and you use as Button .change it in R.id.convert

Categories