Android.widget textView to android.widget.button - java

I am new to Android. I am trying to build this tiny app however every time I add the code in bold:
d = (Button) findViewById (R.id.tvDis);
the app crashes and if I remove this line, everything works fine. I am runing this on android 2.2 api 8 version and nexus s emulator.
package com.maximusstudios.numtowords;
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 {
/**Called when the activity is first created.*/
int counter;
Button add, sub;
TextView d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById (R.id.bAdd);
**d = (Button) findViewById (R.id.tvDis);**
sub = (Button) findViewById(R.id.bSub);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
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.activity_main, menu);
return true;
}
}
XML CODE:
<?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"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your Total is 0"
android:textSize="20dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDis"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bAdd"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSub"
/>
</LinearLayout>
Error list:
02-10 18:00:21.452: D/AndroidRuntime(910): Shutting down VM
02-10 18:00:21.452: W/dalvikvm(910): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-10 18:00:21.472: E/AndroidRuntime(910): FATAL EXCEPTION: main
02-10 18:00:21.472: E/AndroidRuntime(910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maximusstudios.numtowords/com.maximusstudios.numtowords.MainActivity}: java.lang.ClassCastException: android.widget.TextView
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.os.Looper.loop(Looper.java:123)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 18:00:21.472: E/AndroidRuntime(910): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-10 18:00:21.472: E/AndroidRuntime(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-10 18:00:21.472: E/AndroidRuntime(910): at dalvik.system.NativeStart.main(Native Method)
02-10 18:00:21.472: E/AndroidRuntime(910): Caused by: java.lang.ClassCastException: android.widget.TextView
02-10 18:00:21.472: E/AndroidRuntime(910): at com.maximusstudios.numtowords.MainActivity.onCreate(MainActivity.java:23)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 18:00:21.472: E/AndroidRuntime(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-10 18:00:21.472: E/AndroidRuntime(910): ... 11 more

Change to
d = (TextView) findViewById (R.id.tvDis);
Your casting was wrong. With your code, when you do (Button), you are trying to cast the TextView that is returned by findViewById() into a Button, which doesn't work due to them being incompatible types.

You define d as a TextView and try to cast it to a Button
Should be
d = (TextView) findViewById (R.id.tvDis);
The error you're getting is a ClassCastException

Related

.com.process stopped unexpectedly in Android emulator and mobile

I am trying to make a simple android app that will add two numbers and display the output.However, I am getting the following error continuously.
"calculator.com.process stopped unexpectedly"
I used API 10 for rendering layout and API 17 as compiler and Target SDK.
Could post the code finally after all these trouble.
This is the code of main_activity.java
package com.example.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText txtNum1;
EditText txtNum2;
TextView Display;
Button bCal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNum1=(EditText) findViewById(R.id.Num1);
txtNum1=(EditText) findViewById(R.id.Num2);
Display=(TextView)findViewById(R.id.tvDisplay);
bCal=(Button)findViewById(R.id.bAdd);
bCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Tawfiq) {
// TODO Auto-generated method stub
double x=Double.parseDouble(txtNum1.getText().toString());
double y=Double.parseDouble(txtNum2.getText().toString());
double total=x+y;
Display.setText("Sum 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.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bAdd"
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.calculator.MainActivity" >
<EditText
android:id="#+id/Num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Num1"
android:layout_below="#+id/Num1"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/bAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Button" />
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Num2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Sum"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
This is the logcat.
01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130): ... 11 more
You are having problem at line no-27
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
Change the relativeLayouts Id in the xml file.
It should not be same as for the add button.
android:id="#+id/relativeLayout"
Change it as above.
Two id in the same layout cannot be same.
Your add button and the relative layout is having the same id,.
And during the findViewById(), relative layout is being caste in the Button.
That's why class cast exception is coming.
Edit:
I compiled your code:-
the next mistake you have made is-
txtNum1=(EditText) findViewById(R.id.Num2);
It should be
txtNum2=(EditText) findViewById(R.id.Num2);
Mistake-You are not assigning the reference to the second edittext.

Display random month from a button click on android?

I am trying to create an app that lets the user click a button and it will display a random month. I have created a string array in my strings.xml file. Bellow is my main.java, strings.xml and activity.xml. I try to run the app and it just force closes.
package com.example.datebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;
public class Main extends Activity
{
Button btn;
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn1);
final String[] months = getResources().getStringArray(R.array.Months);
final TextView tv =(TextView)findViewById(R.id.text1);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
int rand = (int) (Math.random() * 12);
tv.setText(months[rand]);
}
});
}
}
here is my activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="updateMonth"
android:text="#string/app_name" />
<TextView
android:id="#+id/text1"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="#array/Months" />
</LinearLayout>
here is my strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DateButton</string>
<string-array name="Months">
<item>January</item>
<item>Feburary</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</string-array>
</resources>
I'm still getting errors and the app still says unfortunately your app closed. The logcat is below.
09-11 16:55:17.472: W/Resources(1638): Converting to string: TypedValue{t=0x1/d=0x7f0c0000 a=-1 r=0x7f0c0000}
09-11 16:55:17.512: D/AndroidRuntime(1638): Shutting down VM
09-11 16:55:17.512: W/dalvikvm(1638): threadid=1: thread exiting with uncaught exception (group=0xb3a8fba8)
09-11 16:55:17.532: E/AndroidRuntime(1638): FATAL EXCEPTION: main
09-11 16:55:17.532: E/AndroidRuntime(1638): Process: com.example.datebutton, PID: 1638
09-11 16:55:17.532: E/AndroidRuntime(1638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datebutton/com.example.datebutton.Main}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.os.Looper.loop(Looper.java:136)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-11 16:55:17.532: E/AndroidRuntime(1638): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 16:55:17.532: E/AndroidRuntime(1638): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-11 16:55:17.532: E/AndroidRuntime(1638): at dalvik.system.NativeStart.main(Native Method)
09-11 16:55:17.532: E/AndroidRuntime(1638): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-11 16:55:17.532: E/AndroidRuntime(1638): at com.example.datebutton.Main.onCreate(Main.java:24)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.Activity.performCreate(Activity.java:5231)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 16:55:17.532: E/AndroidRuntime(1638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-11 16:55:17.532: E/AndroidRuntime(1638): ... 11 more
You forgot to add the line: setContentView(R.layout.activity); before trying to instantiate your button object btn.
To use layout in you Activity you have to connect them both together. Below is how your code should look like:
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity);
btn = (Button) findViewById(R.id.btn1);
final String[] months = getResources().getStringArray(R.array.Months);
final TextView tv =(TextView)findViewById(R.id.text1);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
int r, g, b;
int rand = (int) (Math.random() * 12);
tv.setText(months[rand]);
}
});
}

The application sati(process com.example.sati)has stopped unexpectedly.Please try again.[force closed]

my java class consists of following code
package com.example.sati;
import com.example.sati.R;
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 count;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count=0;
add=(Button)findViewById(R.id.but);
sub=(Button)findViewById(R.id.butone);
display=(Button)findViewById(R.id.tvdis);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count++;
display.setText("your total is"+count);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count--;
display.setText("your total is"+count);
}
});
}
#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;
}
}
and my xml file code is as follows;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvdis"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/tot"
android:textSize="25sp"
android:layout_gravity="center"
android:id="#+id/but"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/tet"
android:textSize="25sp"
android:layout_gravity="center"
android:id="#+id/butone" />
</LinearLayout>
but when i run this app it displays the message saying"your app sati has been stoped unexpectedly.please try again[force close] on the emulator.
Log cat shows following errors;
03-23 01:10:30.042: D/AndroidRuntime(476): Shutting down VM
03-23 01:10:30.042: W/dalvikvm(476): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-23 01:10:30.072: E/AndroidRuntime(476): FATAL EXCEPTION: main
03-23 01:10:30.072: E/AndroidRuntime(476): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sati/com.example.sati.MainActivity}: java.lang.ClassCastException: android.widget.TextView
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:123)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 01:10:30.072: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 01:10:30.072: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 01:10:30.072: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 01:10:30.072: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 01:10:30.072: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(NativeMethod)
//application contains two buttons add and sub to add 1 when pressed add and subtract one when presses sub and that increment and decrements of number is displayed as text above the buttons
03-23 01:10:30.072: E/AndroidRuntime(476): Caused by: java.lang.ClassCastException: android.widget.TextView
03-23 01:10:30.072: E/AndroidRuntime(476): at com.example.sati.MainActivity.onCreate(MainActivity.java:26)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 01:10:30.072: E/AndroidRuntime(476): ... 11 more
03-23 01:15:30.154: I/Process(476): Sending signal. PID: 476 SIG: 9
display=(Button)findViewById(R.id.tvdis);
should be
display=(TextView)findViewById(R.id.tvdis);
You declared it as a TextView but trying to cast it as a Button. This is what your exception said Caused by: java.lang.ClassCastException: android.widget.TextView
Also remove one call of setContentView and onCreate. No need to call them twice.

Why does my app "unfortunately stop". All this I want is some simple buttons

This is my very first app. All I need is five buttons, two that call certain phone numbers (only have created one so far) and three buttons that take the user to a certain URL. I have no errors or warnings or any direction on how to navigate the LogCat.
LogCat:
10-08 14:41:40.716: D/AndroidRuntime(793): Shutting down VM
10-08 14:41:40.716: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-08 14:41:40.777: E/AndroidRuntime(793): FATAL EXCEPTION: main
10-08 14:41:40.777: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{acps.mhs/acps.mhs.MainActivity}: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:525)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:41:40.777: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at acps.mhs.MainActivity.onCreate(MainActivity.java:25)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Activity.performCreate(Activity.java:5133)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 14:41:40.777: E/AndroidRuntime(793): ... 11 more
10-08 14:42:19.696: I/Process(793): Sending signal. PID: 793 SIG: 9
Mainactivity.java
package acps.mhs;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button mhshome, pp, mhsdir, cmhs;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
mhshome = (Button) findViewById(R.id.mhshome);
pp = (Button) findViewById(R.id.pp);
mhsdir = (Button) findViewById(R.id.mhsdir);
cmhs = (Button) findViewById(R.id.cmhs);
mhshome.setOnClickListener(this);
pp.setOnClickListener(this);
mhsdir.setOnClickListener(this);
cmhs.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.mhshome:
Uri uri = Uri.parse("http://www2.k12albemarle.org/school/mohs/Pages/default.aspx");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case R.id.pp:
Uri uri2 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(intent2);
break;
case R.id.mhsdir:
Uri uri3 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
startActivity(intent3);
break;
case R.id.cmhs:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);
break;
}
}
#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
<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=".MainActivity" >
<Button
android:id="#+id/pp"
style="#style/AppBaseTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhshome"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/pp"/>
<Button
android:id="#+id/mhshome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="onClick"
android:text="#string/mhshome" />
<Button
android:id="#+id/mhsdir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/mhsdir" />
<Button
android:id="#+id/cmhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhsdir"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/callmhs" />
</RelativeLayout>
Its gonna be something incredibly simple I'm certain. Please point me in the right direction.
You need to call setContentView(R.layout.activity_main) :) That's why findViewById is returning null on your views.
You are not setting your layout.
Call setContentView(layout) in your onCreate() method.

Unfortunately app has stopped working

I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.

Categories