I'm just learning android here, and Java to be honest. I'm just trying to switch to a different activity via a button click, however, it keeps crashing. It crashes when I click the button and go to make the switch. Can someone please help me figure where I'm going wrong?
First Activity:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class TicTacToe extends Activity {//Start TicTactToe Class
//CONSTANTS
//Variables
Button mainButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
mainButton = (Button) findViewById(R.id.bPlayNow);
mainButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), playerMenu.class);
startActivity(i);
}
});
}
}//End TicTacToe Class
Activity I'm jumping to:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class playerMenu extends Activity {
Button bOnePlayer, bTwoPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bTwoPlayer = (Button) findViewById(R.id.bTwoPlayer);
bTwoPlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the XML for the first activity:
<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="#000000"
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=".TicTacToe" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="50dp"
android:text="Welcome to \nTic-Tac-Toe"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bPlayNow"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textSize="34dp"
android:text="Play now!" />
</RelativeLayout>
XML for the second activity:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_x="78dp"
android:layout_y="30dp"
android:text="GAME MODE"
android:textColor="#FFFFFF"
android:textSize="50dp" />
<Button
android:id="#+id/bOnePlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="160dp"
android:text="Single Player"
android:textSize="25dp" />
<Button
android:id="#+id/bTwoPlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="220dp"
android:text="Two Player"
android:textSize="25dp" />
</AbsoluteLayout>
For starting a new activity, it is necessary to add it in the manifest file.
<application >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
then in onCreate(), for starting a new activity when the button is pressed, you can do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TicTacToe.this, playerMenu.class);
startActivity(intent);
}
});
}
Make sure that you define playerMenu in the AndroidManifest file.
<application ... >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
Check this page for more information
http://developer.android.com/training/basics/firstapp/starting-activity.html
Are you sure that you have register your PlayerMenu activity in AndroidManifest.xml ?
...
please paste the error log.
use this code in your first Activity to jump to another
onClick(View v){
Intent ps = new Intent (TicTacToe.this,
PlayerMenu.class);
startActivity(ps);
}
Manifest File :
<Activity
android:name="com.example.killacatoe.TicTacToe">
</Activity>
Related
I'm beginner in Android. I'm trying to make an app where when user tap on button he should go to another activity let say if user tap on button 1 he see new activity with information-A and if he press button 2 he see information-B in same activity he saw information A.
How can I set that.
If I am understanding your question correctly, here's a solution.
You can use the putExtras method of Intent to pass data in key/value pairs to the next activity.
Add the following to activity 1 layout:
<Button
android:id="#+id/btnA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button A"
android:onClick="goToActivityFromButtonA" />
<Button
android:id="#+id/btnB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btnA"
android:text="Button B"
android:onClick="goToActivityFromButtonB" />
Add the following methods to Activity 1:
public void goToActivityFromButtonA(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button A");
startActivity(intent);
}
public void goToActivityFromButtonB(View view) {
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("buttonData", "You clicked button B");
startActivity(intent);
}
Add the following to Activity 2 layout:
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Add the following to the onCreate method of activity 2:
TextView txtText = (TextView)findViewById(R.id.txtText);
txtText.setText(getIntent().getExtras().getString("buttonData"));
I just created a sample project for you:
https://drive.google.com/file/d/0B0S6sddMC_rMSUNieUxkR2lFRzQ/view?usp=sharing
you have first create ui for first activity like this
<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=".FirstActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="77dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="110dp"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/button1"
android:text="Button 2" />
</RelativeLayout>
ui of second activity like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second actvity" />
</LinearLayout>
activity first code where i set index with intent for both button click(you also put string double ling and message with intent)
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id){
case R.id.button1:
Intent i1=new Intent(FirstActivity.this,secondactivity.class);
i1.putExtra("one", 1);
startActivity(i1);
break;
case R.id.button2:
Intent i2=new Intent(FirstActivity.this,secondactivity.class);
i2.putExtra("one", 2);
startActivity(i2);
break;
}
}
}
and second activity i get that integer value to identify which button is clicked and fire operation on that condition
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class secondactivity extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
tv=(TextView)findViewById(R.id.textView1);
Bundle extras = getIntent().getExtras();
int index = extras.getInt("one");
if(index==1){
tv.setText("nformation-A "+index);
}else if(index==2){
tv.setText("nformation-B "+index);
}
}
}
I am new to android. In my app, having common header.
I have created common header.xml and include that header.xml in all the activites.
But the button clicks [listener] not working.
How to solve the issue. this is the code.
Header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Home" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Accounts" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="History" />
</LinearLayout>
Home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
<EditText
android:id="#+id/txtOTPCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="verify" />
</LinearLayout>
BaseHeaderActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class BaseHeaderActivity extends Activity
{
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
System.out.println("Home button Action");
}
});
}
}
Home Activity.java
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends BaseHeaderActivity
{
Button btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btnVerify = (Button)findViewById(R.id.btnVerify);
}
}
base activities button clicks not working..!
Kindly suggest the solution.
Thanks in advance,
Arun
You don't have to create two different activities for this. Create one activity and use all buttons (including button in header.xml) similarly
public class MainActivity extends Activity {
Button btn1,btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Home button Action", Toast.LENGTH_SHORT).show();
}
});
btnVerify = (Button)findViewById(R.id.btnVerify);
btnVerify.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Button Verify Action", Toast.LENGTH_SHORT).show();
}
});
}
}
You can not use one layout xml file with single activity only.
Note - If you still want to create two different activities - one base and one child then you need to use setContentView() method in base activity with main layout file (Home.xml in your case) and extend it with child activity. Remember in this case dont use setContentView() method in Child Activity. This scenario is not preferred generally unless we want to create several child of same activity.
I have 3 activities which I want to use different backgrounds with. but when i do this i get this i get this error however if i only use different backgrounds on 2 activities then the app works
11-20 13:40:25.855: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medepad.community_virtual_ward/com.medepad.community_virtual_ward.Temperature}: android.view.InflateException: Binary XML file line #2: Error inflating class
the activities are called main welcome and temperature.
code for 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"
android:background="#drawable/main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
int a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView start = (TextView)findViewById(R.id.textView1);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent welcome= new Intent (this,Welcome.class);
startActivity(welcome);
}
}
code for temperature
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/temperature" >
</LinearLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Temperature extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.temperature);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
welcome xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/welcome" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="400dp"
android:layout_marginTop="115dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="40dp" />
</RelativeLayout>
java code
package com.medepad.community_virtual_ward;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Welcome extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView next= (TextView)findViewById(R.id.textView2);
next.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent temperature= new Intent(this, Temperature.class);
startActivity(temperature);
}
}
why am i getting this error and what can i do to use the amount of backgrounds i want?
The end of your first XML file has an extra >.
Please check the following file
temperature.xml
Make sure, it has following contents
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/temperature" >
</LinearLayout>
and you have either defined a drawable resource with name temperature.xml and main.xml under res/drawable folder or you have added image resources with name temperature and main in your Android project.
I found myself in trouble with creating new activity, I get the unfortunately your app has stopped error message and as a good humanbeing I thought I'd share my misfortune with you:
I can't get my button to open new activity:
MainActivity.java:
package com.example.vogella.dev;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton =(RadioButton)findViewById(R.id.radio0);
RadioButtonfahrenheitButton=(RadioButton)findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this,getResources().getString(R.string.toast_a),
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(Strgin.valuof(ConvertFahrenheitToCelsius(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
} else {
text.setText(Strgin.valuof(ConvertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
private float ConvertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) +32;
}
public void scrollview(View v) {
Intent intent =newIntent(this,ScrollviewActivity.class);
startActivity(intent);
}
}
My activity_main.xml
<RelativeLayoutxmlns: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="#color/myColor" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:inputType="numberDecimal|numberSigned" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1">
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/celsius" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fahrenheit" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/radioGroup1"
android:onClick="onClick"
android:text="#string/calc" />
<Button
android:id="#+id/test_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="44dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/test_button"
android:onClick="scrollview"/>
</RelativeLayout>
The AndroidManifest in portion
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
android:parentActivityName=com.example.vogella.dev.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.vogella.dev.MainActivity" />
</activity>
</application>
And the activity I'm trying to open
public class ScrollviewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollview);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 500; i++) {
s += "vogella.com" ;
}
view.setText(s);
}
}
Intent intent =newIntent(this,ScrollviewActivity.class);
You forgot the space between new and Intent. However, you may have other problems as well. It would help to tell us the error thrown.
What is the error you are getting in the logcat. May be you have not mentioned your ScrollViewActivity in your Manifest file.Please check
Your manifest must contain the MainActivity:
<activity android:name="com.example.vogella.dev.MainActivity" />
1)use this in onCreate()
Button button = (Button) findViewById(R.id.button1);
you did initialize your EditText then why didnt you do it for your Button named 'button1'
2) even mention your ScrollViewActivity in android manifest
3) Intent i = new Intent(getApplicationContext,ScrollViewActivity.class);
startActivity(i);
Have you declare new Activity in Manifest? If not, add it like this:
<activity android:name="com.example.vogella.dev.ScrollviewActivity" />
How even you compile does IDE not implemented an error
Intent intent =newIntent(this,ScrollviewActivity.class); // Error is here
startActivity(intent);
It should be like this
Intent intent =new Intent(this,ScrollviewActivity.class);
startActivity(intent);
And also never forget to adding your every new activity to Manifest
<activity
android:name="com.activities.ActivitySettings"
android:icon="#drawable/icon_small"
android:label="#string/title_activity_activity_settings"
android:logo="#drawable/icon_small"
android:theme="#style/MyTheme" >
</activity>
Specifically for your question
You have two activity MainActivity and ScrollViewActivity
on manifest you mention this
<activity
android:name="com.example.vogella.dev.Scrollview"
android:label="#string/title_activity_scrollview"
Scrollview is not any of your activity but ScrollViewActivity is
Public void launchActivity(View view){
Intent intent = new Intent(this, youractivityname
Class);
startActivity(intent);
This is the java code and xml file for a program I am writing, and it force closes whenever i try to invoke the plusCalc button in-program. Could someone please tell me why?
Thank you!
Java file:
package org.example.knittingframe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class KnittingFrame extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View plusCalc = findViewById(R.id.plus_calc_button);
plusCalc.setOnClickListener(this);
View exitbutton = findViewById(R.id.exit_button);
exitbutton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.plus_calc_button:
startPlusCalc();
break;
case R.id.exit_button:
finish();
break;
}
}
public void startPlusCalc() {
Intent i = new Intent(this, PlusCalc.class);
startActivity(i);
}
}
Here is the XML file:
<?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">
<TextView
android:text="#string/main_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="#+id/plus_calc_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/plusCalc_label"
android:layout_gravity="center" />
<Button
android:id="#+id/exit_button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Exit"
android:layout_gravity="center" />
</LinearLayout>
Without the stack trace it appears that the problem is with declaring the PlusCalc.class class in the App Manifest file, because as you say, the error only appears when you click on the PlusCalc button.
If this doesn't help please post the stack trace.