I cannot figure out why these 2 buttons are not working, i have a layout file inwhich is created when the user selects a certain theme in my application. With the layout it has a webview it has 2 buttons to goback and goforward.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward">
</Button>
<WebView
android:id="#+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.12" >
This is not the entire layout file, but here is the bit of stuff i am working with for the webview and the 2 buttons, now inside my main activity here is my button code.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else if (Prefs.theme.equals("Theme2"))
setContentView(R.layout.main2);
else if (Prefs.theme.equals("Theme3"))
setContentView(R.layout.main3);
else setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.button1);
btnBackward=(Button) findViewById (R.id.button2);
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// more code within on create .....
// Later in the code
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.button2:
WebViewClientDemoActivity.web.goForward();
break;
}
//
I problem im having is by default it loads main.xml not main3 (which is were the 2 buttons are)
LogCat Errors
07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:123)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 16:00:34.802: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 16:00:34.802: E/AndroidRuntime(547): ... 11 more
First in your onCreate method find your buttons by ids :
public class YourActivity extends Activity implements OnClickListener {
Button btnForward;
Button btnBackward;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.btnForward);
btnBackward=(Button) findViewById (R.id.btnBackward);
//set listeners
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// your code here ....
}
#Override
public void onClick(View v ) {
switch(v.getId()) {
case R.id.btnBackward:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.btnForward:
WebViewClientDemoActivity.web.goForward();
break;
}
}
}
It should work. Make sure you don't have 2 buttons with the same #id.
It's a common issue when you multiplicate your buttons from one. (copy/paste)
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="#drawable/button_blue"
style="#style/ButtonText" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/button_blue"
style="#style/ButtonText"
android:text="Forward" />
try this.
then later in java code, do this
Button btnNext = (Button) findViewById(R.id.button2);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goForward();
}
});
Button btnBack = (Button) findViewById(R.id.button1);
btnNext.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
WebViewClientDemoActivity.web.goBack();
}
});
Related
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.
I have a very very simple project in which what I am trying to achieve is to that when a user click on the button it display the text. But in the ADB it says unfortunately showtext stop working. What is the reason behind this?
I had tried but not got any result. My code is fine but not working. Below is java code for it. I omitted xml code as it contain only the button and a text view not so complex.
public class MainActivity extends Activity {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText=(Button)findViewById(R.id.bShowText);
}
void ShowMeText(View view){
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.VISIBLE);
}
}
Try solve my this little issue. Thanks in Advance.
As someone asked for logcat error here is it -
06-11 06:51:32.376: E/AndroidRuntime(1223): FATAL EXCEPTION: main
06-11 06:51:32.376: E/AndroidRuntime(1223): java.lang.IllegalStateException: Could not find a method ShowMeText(View) in the activity class com.mylearning.showtext.MainActivity for onClick handler on view class android.widget.Button with id 'bShowText'
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$1.onClick(View.java:3620)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View.performClick(View.java:4240)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$PerformClick.run(View.java:17721)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Handler.handleCallback(Handler.java:730)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.os.Looper.loop(Looper.java:137)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.reflect.Method.invoke(Method.java:525)
06-11 06:51:32.376: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-11 06:51:32.376: E/AndroidRuntime(1223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 06:51:32.376: E/AndroidRuntime(1223): at dalvik.system.NativeStart.main(Native Method)
06-11 06:51:32.376: E/AndroidRuntime(1223): Caused by: java.lang.NoSuchMethodException: ShowMeText [class android.view.View]
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.Class.getConstructorOrMethod(Class.java:423)
06-11 06:51:32.376: E/AndroidRuntime(1223): at java.lang.Class.getMethod(Class.java:787)
06-11 06:51:32.376: E/AndroidRuntime(1223): at android.view.View$1.onClick(View.java:3613)
06-11 06:51:32.376: E/AndroidRuntime(1223): ... 11 more
You need to add android:onClick="ShowMeText" in your Button in your Layout. like so
<Button .............
android:onClick="ShowMeText"
......... />
and used lower case for method creation as Java naming Convention . like so showmetext
and also defined your ShowMeText(...) method as Public
ShowMeText(View v) has to be public.
Btw. start function names with lower case characters as specified by Java code style.
<Button
android:id="#+id/tvShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ShowMeText" />
in java code
public void ShowMeText(View v){
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.VISIBLE);
}
public class MainActivity extends Activity implements OnClickListener {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText = (Button) findViewById(R.id.bShowText);
DisplayText = (TextView) findViewById(R.id.tvShowText);
ShowText.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == ShowText.getId())
// need to check ID because from which view has fire event soo we can handle appropriate
DisplayText.setVisibility(View.VISIBLE);
}
}
// Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="#+id/bShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowText"
android:onClick="ShowMeText"/>
<TextView
android:id="#+id/tvShowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="gone"
android:text="TextView With Some Text"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private TextView tvShowText;
private Button bShowText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvShowText = (TextView)findViewById(R.id.tvShowText);
bShowText = (Button)findViewById(R.id.bShowText);
}
public void ShowMeText(View v){
if(v.getId() == bShowText.getId()) {
if(tvShowText.getVisibility() == View.VISIBLE){
tvShowText.setVisibility(View.GONE);
}else{
tvShowText.setVisibility(View.VISIBLE);
}
}
}
}
Try this:
public class MainActivity extends Activity {
Button ShowText;
TextView DisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShowText=(Button)findViewById(R.id.bShowText);
DisplayText=(TextView)findViewById(R.id.tvShowText);
DisplayText.setVisibility(View.Gone);
ShowText.setonClickListener(this);
}
public void ShowMeText(View view){
DisplayText.setVisibility(View.VISIBLE);
DisplayText.setText("Show your Text");
}
}
This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
When I try to open this page it crashes. I have tried to make this button count various ways and can't get it to count! I have tried all kinds of tutorials by thenewboston and other people but it doesn't work for me.
Medical.java
package com.fullcyclestudios.rust;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class Medical extends ActionBarActivity {
Button add, sub;
int counter;
TextView textView;
TextView quantityDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
quantityDisplay = (TextView) findViewById(R.id.textViewQuantityMedical);
counter=1;
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
counter++;
quantityDisplay.setText("it"+counter);
}
});
sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
counter--;
quantityDisplay.setText("it"+counter);
}
});
Intent intent = getIntent();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.medical, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_medical,
container, false);
return rootView;
}
}
/** Bandage button */
public String bandageMaterials(View view)
{
String materials = "Bandage\r\n\r\n2 Cloth";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Small Medkit button */
public String smallMedkitMaterials(View view)
{
String materials = "Small Medkit\r\n\r\n2 Cloth\r\n2 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Large Medkit button */
public String largeMedkitMaterials(View view)
{
String materials = "Large Medkit\r\n\r\n3 Cloth\r\n3 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
}
fragment_medical.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.fullcyclestudios.rust.Medical$PlaceholderFragment" >
<ScrollView
android:id="#+id/scrollViewMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/bandageButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/bandage"
android:onClick="bandageMaterials" />
<Button
android:id="#+id/smallMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/smallMedkit"
android:onClick="smallMedkitMaterials" />
<Button
android:id="#+id/largeMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/largeMedkit"
android:onClick="largeMedkitMaterials" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textViewMatMedical"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginTop="140dp"
android:background="#color/gray"
android:text="#string/blank" />
<TextView
android:id="#+id/textViewQuantityMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minusButtonMedical"
android:layout_alignRight="#+id/minusButtonMedical"
android:layout_below="#+id/addButtonMedical"
android:gravity="center_horizontal"
android:text="#string/one"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="25sp" />
<Button
android:id="#+id/minusButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="80dp"
android:layout_alignLeft="#+id/addButtonMedical"
android:layout_alignRight="#+id/addButtonMedical"
android:text="#string/minus"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/addButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="#+id/textViewMatMedical"
android:layout_alignRight="#+id/textViewMatMedical"
android:text="#string/add"
android:textSize="30sp" />
</RelativeLayout>
ERRORS
06-08 16:10:38.890: W/dalvikvm(855): threadid=1: thread exiting with uncaught exception (group=0xb2a40ba8)
06-08 16:10:38.990: E/AndroidRuntime(855): FATAL EXCEPTION: main
06-08 16:10:38.990: E/AndroidRuntime(855): Process: com.fullcyclestudios.rust, PID: 855
06-08 16:10:38.990: E/AndroidRuntime(855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fullcyclestudios.rust/com.fullcyclestudios.rust.Medical}: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Looper.loop(Looper.java:136)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 16:10:38.990: E/AndroidRuntime(855): at dalvik.system.NativeStart.main(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): Caused by: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at com.fullcyclestudios.rust.Medical.onCreate(Medical.java:33)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Activity.performCreate(Activity.java:5231)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-08 16:10:38.990: E/AndroidRuntime(855): ... 11 more
06-08 16:10:43.330: I/Process(855): Sending signal. PID: 855 SIG: 9
Your issue is here:
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
but from what you post: addButtonMedical and minusButtonMedical are inside fragment_medical and not activity_medical xml layout!
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.
public class screen2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
final Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//AInteger.parseInt(string)
System.out.println("1");
EditText et = (EditText)findViewById(R.id.editText1);
System.out.println("2");
int zipCode = Integer.parseInt(et.getText().toString());
System.out.println("3");
System.out.println(zipCode);
System.out.println("dude...5");
}
});
}
}
This code shows the errror in the logcat :
11-13 19:58:06.806: W/KeyCharacterMap(281): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-13 19:58:09.471: I/System.out(281): 1
11-13 19:58:09.471: I/System.out(281): 2
11-13 19:58:09.477: D/AndroidRuntime(281): Shutting down VM
11-13 19:58:09.477: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-13 19:58:09.527: E/AndroidRuntime(281): FATAL EXCEPTION: main
11-13 19:58:09.527: E/AndroidRuntime(281): java.lang.NullPointerException
11-13 19:58:09.527: E/AndroidRuntime(281): at com.example.andtwi.screen2$1.onClick(screen2.java:23)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.view.View.performClick(View.java:2408)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.view.View$PerformClick.run(View.java:8816)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Handler.handleCallback(Handler.java:587)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
11-13 19:58:09.527: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-13 19:58:09.527: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 19:58:09.527: E/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
11-13 19:58:09.527: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-13 19:58:09.527: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-13 19:58:09.527: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
Though it looks like an Null Pointer exception , i think i am doing it things exactly i found on forums. Here is code of my related xml file as well. Can anyone suggest where am i doing it wrong?
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Zipcode" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="5" >
<requestFocus />
</EditText>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get movies from Flixster" />
</LinearLayout>
The code breaks at this as well :
int zipCode = Integer.parseInt(et.getText().toString());
Try this:
Button button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("1");
System.out.println("2");
String zipCode = et.getText().toString();
System.out.println("3");
System.out.println(zipCode);
}
}
I think,you don't need to write String zipCode = (String)et.getText().toString(); there as it is already returns a String object.
And hope,you don't miss to include setContentView(R.layout.your_xml_file); before declaring a Button and EditText.
Edit 1:
You don't seem to get problem with
int zipCode=Integer.parseInt(et.getText().toString()); there.Try this with above code modification.
You might be getting null pointer exception because you are getting String s=et.getText().toString() as null.Please check it for null before casting it to int.
Button button1 = (Button)findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("1");
System.out.println("2");
String check = et.getText().toString();
int zipCode=0;
if(!check.equals(""))
zipCode=Integer.parseInt(check);
System.out.println("3");
System.out.println(zipCode);
}
}
Could you post your onCreate method, as well as line 23 on its own? The most likely problem is that you neglect to call setContentView and that is why findViewById returns null.