Here's the java code for the class
package com.warriorsoft.mtgupanddown;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
int guy1;
int guy2;
MediaPlayer sound;
Button plus1g1, plus1g2, min1g1, min1g2, plus5g1, plus5g2, min5g1, min5g2,
GG, RESET;
TextView g1, g2;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
guy1 = 20;
guy2 = 20;
min5g1 = (Button) findViewById(R.id.button1); // x
plus5g1 = (Button) findViewById(R.id.button2); //x
plus1g1 = (Button) findViewById(R.id.button3); // x
min1g1 = (Button) findViewById(R.id.button4); // x
min5g2 = (Button) findViewById(R.id.button5); // x
plus5g2 = (Button) findViewById(R.id.button6); //x
min1g2 = (Button) findViewById(R.id.button7); // x
plus1g2 = (Button) findViewById(R.id.button8); //x
RESET = (Button) findViewById(R.id.RESET);
GG = (Button) findViewById(R.id.GG);
g1 = (TextView) findViewById(R.id.tvPlayer1HP);
g2 = (TextView) findViewById(R.id.tvPlayer2HP);
min5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
guy1 -= 5;
g1.setText(guy1);
}
});
min5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 5;
g2.setText(guy2);
}
});
min1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 -= 1;
g1.setText(guy1);
}
});
min1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 -= 1;
g2.setText(guy2);
}
});
plus5g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 5;
g2.setText(guy2);
}
});
plus5g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 5;
g1.setText(guy1);
}
});
plus1g1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy1 += 1;
g1.setText(guy1);
}
});
plus1g2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
guy2 += 1;
g2.setText(guy2);
}
});
RESET.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
guy1 = 20;
guy2 = 20;
g2.setText(guy2);
g1.setText(guy1);
}
});
GG.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0){
sound = MediaPlayer.create(MainActivity.this, R.raw.gg );
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Here's the layout xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tvPlayer2HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="36dp"
android:text="20"
android:textSize="35dp" />
<TextView
android:id="#+id/tvPlayer1HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:text="20"
android:textSize="35dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/button1"
android:text="+5" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:text="-1" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/button1"
android:layout_marginBottom="28dp"
android:text="+1" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignParentRight="true"
android:text="+5" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/button6"
android:text="-5" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button5"
android:layout_alignTop="#+id/button4"
android:text="+1" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_toRightOf="#+id/button5"
android:text="-1" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/button5"
android:ems="10"
android:hint="Guy 1" >
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/button1"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Guy 2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvPlayer1HP"
android:layout_marginTop="74dp"
android:text="-5" />
<ImageView
android:id="#+id/GG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvPlayer2HP"
android:layout_centerHorizontal="true"
android:minHeight="140dp"
android:minWidth="120dp"
android:src="#drawable/gg" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="Optimal sideways"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/RESET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="RESET" />
</RelativeLayout>
And here's the logcat
01-16 14:01:51.292: W/dalvikvm(699): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-16 14:01:51.302: E/AndroidRuntime(699): FATAL EXCEPTION: main
01-16 14:01:51.302: E/AndroidRuntime(699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.warriorsoft.mtgupanddown/com.warriorsoft.mtgupanddown.MainActivity}: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:123)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:507)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-16 14:01:51.302: E/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-16 14:01:51.302: E/AndroidRuntime(699): at dalvik.system.NativeStart.main(Native Method)
01-16 14:01:51.302: E/AndroidRuntime(699): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-16 14:01:51.302: E/AndroidRuntime(699): at com.warriorsoft.mtgupanddown.MainActivity.onCreate(MainActivity.java:33)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 14:01:51.302: E/AndroidRuntime(699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-16 14:01:51.302: E/AndroidRuntime(699): ... 11 more
01-16 14:01:53.712: I/Process(699): Sending signal. PID: 699 SIG: 9
The problem is running the code on a phone.
It tells me that the program broke in some unexpected way and forces me to force close.
If anyone could please help me understand what i've done wrong i'll be eternally grateful
I am still new to programming and can barely understand the logcats
please go easy
Thankyou for trying
You are finding the ImageView and casting it to Button.
check this line of code.
GG = (Button) findViewById(R.id.GG);
Change the type of GG as ImageView and typecast to ImageView as below:
ImageView GG;
GG = (ImageView) findViewById(R.id.GG);
Change this to this
GG = (Button) findViewById(R.id.GG);
to
GG = (ImageView) findViewById(R.id.GG);
you cannot cast an ImageView to a Button. Which is what you are doing
Related
I'm trying to make it so that once the user enters there name into the given EditText then accesses guessing game from the main menu it redirects you to new Activity in this case the Guessing game but it will not carry over the Information To TextView.
Here is my Code for GameMenu.java:
public class GameMenu extends Activity {
private static final int REQUEST_CODE = 10;
// TextViews
TextView Help;
// String's
String GameSelec;
//Intent
Intent Intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamemenu);
Help = (TextView) findViewById(R.id.TextHelp);
Help.setVisibility(View.INVISIBLE);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menuoptions, menu);
return true;
}
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.
switch (item.getItemId()) {
case R.id.help:
Help = (TextView) findViewById(R.id.TextHelp);
Help.setText("Enter your name and Select game to play");
Help.setVisibility(View.VISIBLE);
Handler handle = new Handler();
handle.postDelayed(new Runnable() {
#Override
public void run() {
Help.setVisibility(View.INVISIBLE);
}
}, 10000);
return true;
case R.id.GuesGame:
Toast.makeText(this, "Redirecting Now",
Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(GameMenu.this, GuessGame.class);
EditText EditName = (EditText) findViewById(R.id.EditName);
myIntent.putExtra("Name", EditName.getText().toString());
startActivityForResult(myIntent, REQUEST_CODE);
return true;
case R.id.Game2:
Toast.makeText(this, "In Development",
Toast.LENGTH_LONG).show();
return true;
case R.id.Game3:
Toast.makeText(this, "In Development",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Then this is my gamemenu.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GameMenu" >
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alignmentMode="alignBounds"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="14"
android:useDefaultMargins="true" >
<TextView
android:id="#+id/Gamemenu"
android:layout_column="1"
android:layout_gravity="center_horizontal|top"
android:layout_row="1"
android:text="Game Menu: Follow Instructions"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/GameInsruct"
android:layout_column="1"
android:layout_gravity="center_horizontal|top"
android:layout_row="2"
android:text="Please enter name and select game"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/EditName"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="3"
android:ems="10"
android:hint="Your name here"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/TextMessage"
android:layout_column="1"
android:layout_gravity="center"
android:layout_row="13"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/TextHelp"
android:layout_column="1"
android:layout_gravity="center_horizontal|top"
android:layout_row="13"
android:text="Help"
android:textAppearance="?android:attr/textAppearanceMedium" />
</GridLayout>
</LinearLayout>
Then GuessGame.java:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class GuessGame extends Activity {
// TextViews
TextView Welcome;
TextView Txtname;
TextView Txtnumber;
// EditViews
EditText EdtName;
EditText EditNum1;
EditText EditNum2;
EditText EditGuess;
// Buttons
Button BtnDisp;
// RadioGroup-Related
private RadioGroup radioGroup;
private RadioButton easy, medium, hard;
// int's
int Max;
int max;
int random;
int Attempts;
int guess;
int num1;
// Strings
String name;
String result;
String Strngnum1;
String NameExtra;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Intent Intent = getIntent();
if (Intent.hasExtra(NameExtra)){
TextView setName = (TextView) findViewById(R.id.EdtName);
String NameExtra = Intent.getStringExtra("Player");
setName.setText("Player:" + NameExtra + "Have Fun");
}
//name = NameExtra;
/*Bundle extras = getIntent().getExtras();
if (extras !=null) {
NameExtra = extras.getString("Name");
if (NameExtra != null) {
TextView setName = (TextView) findViewById(R.id.EditName);
setName.setText("Player:" + NameExtra + "Have Fun");
name = NameExtra;
}
}
*/
/*
*/
Attempts = Attempts + 3;
//EditName = (EditText) findViewById(R.id.EdtName);
EditNum1 = (EditText) findViewById(R.id.EdtNum1);
Txtname = (TextView) findViewById(R.id.UserDisp);
radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if (checkedId == R.id.Easy) {
Toast.makeText(getApplicationContext(), "Choice: 1-5",
Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.Medium) {
Toast.makeText(getApplicationContext(), "Choice: 1-10",
Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.Hard) {
Toast.makeText(getApplicationContext(), "Choice: 1-20",
Toast.LENGTH_SHORT).show();
}
}
});
easy = (RadioButton) findViewById(R.id.Easy);
medium = (RadioButton) findViewById(R.id.Medium);
hard = (RadioButton) findViewById(R.id.Hard);
}
public void Start(View StartDisp) {
int selectedId = radioGroup.getCheckedRadioButtonId();
//Txtname = (TextView) findViewById(R.id.UserDisp);
//Txtname.setText(name);
// find which radio Button is checked by id
if (selectedId == easy.getId()) {
Toast.makeText(getApplicationContext(), "You Chose 'Easy' '1-5'",
Toast.LENGTH_LONG).show();
getWindow().getDecorView().setBackgroundColor(
Color.parseColor("#abf234"));
max = 5;
} else if (selectedId == medium.getId()) {
Toast.makeText(getApplicationContext(),
"You Chose 'Medium' '1-10'", Toast.LENGTH_LONG).show();
getWindow().getDecorView().setBackgroundColor(
Color.parseColor("#E8A317"));
max = 10;
} else {
Toast.makeText(getApplicationContext(), "You Chose 'Hard' '1-20'",
Toast.LENGTH_LONG).show();
getWindow().getDecorView().setBackgroundColor(
Color.parseColor("#FF0000"));
max = 20;
}
random = RandomNumber(max);
//Txtname.setText(name + random );
Txtname = new TextView(this);
Txtnumber = new TextView(this);
TextView DispInform = new TextView(this);
EdtName = (EditText) findViewById(R.id.EdtName);
EditNum1 = (EditText) findViewById(R.id.EdtNum1);
Txtname = (TextView) findViewById(R.id.UserDisp);
Txtname = (TextView) findViewById(R.id.EditName);
Txtname.setText(name);
Txtnumber =(TextView) findViewById(R.id.EdtNum1);
num1 = Integer.parseInt(Txtnumber.getText().toString());
Strngnum1 = String.valueOf(num1);
StringBuilder StrngBuild = new StringBuilder();
StrngBuild.append(name);
StrngBuild.append(" You have Chosen the number");
StrngBuild.append(Strngnum1);
line 181 StrngBuild.append(random);
DispInform = (TextView) findViewById(R.id.DispAll);
DispInform.setText(StrngBuild);
}
public void Guess(View BtnGuess) {
TextView tvResult = (TextView) findViewById(R.id.tvResult);
if (Attempts > 0) {
EditGuess = (EditText) findViewById(R.id.EdtGuess);
guess = Integer.parseInt(EditGuess.getText().toString());
if (guess == random) {
result = "Correct";
tvResult.setText(result);
getWindow().getDecorView().setBackgroundColor(
Color.parseColor("#FFFFFF"));
}
else if ((guess + 3 == random) || (guess - 3 == random)) {
Attempts = Attempts - 1;
result = "Almost There";
tvResult.setText(result);
}
else if (guess - 6 == random) {
Attempts = Attempts - 1;
result = "Sky High that one";
tvResult.setText(result);
}
else if (guess + 6 == random) {
Attempts = Attempts - 1;
result = "Low blow guess that one";
tvResult.setText(result);
}
else
{
Attempts = Attempts - 1;
}
} else if (Attempts == 0) {
Toast.makeText(getApplicationContext(), "Out Of Attempts",
Toast.LENGTH_LONG).show();
getWindow().getDecorView().setBackgroundColor(
Color.parseColor("#43C6DB"));
}
}
public int RandomNumber(int Max) {
int random = 1 + (int) (Math.random() * ((Max - 1) - 1));
return random;
}
/*
* public void generateRandom() { random =
* Min+(int)(Math.random()*((Max-Min) + 1));
*
* if(random > Max || random < Min) { generateRandom(); }
*
* }
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gamemain, 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);
}
}
game.xml:
<TextView
android:id="#+id/Welcome"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="1"
android:text="Welcome:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/EntrNum"
android:layout_width="167dp"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="left"
android:layout_row="3"
android:layout_rowSpan="2"
android:text="Please enter a number between 1 - 20"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/EdtName"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="left|bottom"
android:layout_row="6"
android:ems="10"
android:hint="Name"
android:text="" >
</TextView>
<EditText
android:id="#+id/EdtNum1"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="left"
android:layout_row="8"
android:ems="10"
android:hint="Number"
android:inputType="number" >
<requestFocus />
</EditText>
<RadioGroup
android:id="#+id/myRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:checkedButton="#+id/sound" >
<RadioButton
android:id="#+id/Easy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-5" />
<RadioButton
android:id="#+id/Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-10" />
<RadioButton
android:id="#+id/Hard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1-20" />
</RadioGroup>
<Space
android:layout_width="32dp"
android:layout_height="1dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="32dp"
android:layout_column="0"
android:layout_row="0" />
<EditText
android:id="#+id/EdtGuess"
android:layout_width="157dp"
android:layout_column="1"
android:layout_gravity="left"
android:layout_row="15"
android:ems="10"
android:hint="Take A Guess"
android:inputType="number" />
<Button
android:id="#+id/StartDisp"
android:layout_width="164dp"
android:layout_height="83dp"
android:layout_column="2"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="14"
android:onClick="Start"
android:text="Start Game" />
<Button
android:id="#+id/BtnGuess"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_gravity="right|top"
android:layout_row="17"
android:onClick="Guess"
android:text="Guess" />
<Space
android:layout_width="226dp"
android:layout_height="1dp"
android:layout_column="6"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="16dp"
android:layout_column="0"
android:layout_row="2" />
<Space
android:layout_width="1dp"
android:layout_height="16dp"
android:layout_column="0"
android:layout_row="7" />
<Space
android:layout_width="1dp"
android:layout_height="16dp"
android:layout_column="0"
android:layout_row="9" />
<Space
android:layout_width="1dp"
android:layout_height="2dp"
android:layout_column="0"
android:layout_row="10" />
<Space
android:layout_width="1dp"
android:layout_height="2dp"
android:layout_column="0"
android:layout_row="11" />
<Space
android:layout_width="1dp"
android:layout_height="4dp"
android:layout_column="0"
android:layout_row="12" />
<Space
android:layout_width="1dp"
android:layout_height="2dp"
android:layout_column="0"
android:layout_row="13" />
<Space
android:layout_width="1dp"
android:layout_height="78dp"
android:layout_column="0"
android:layout_row="15" />
<Space
android:layout_width="1dp"
android:layout_height="219dp"
android:layout_column="0"
android:layout_row="17" />
<Space
android:layout_width="1dp"
android:layout_height="53dp"
android:layout_column="0"
android:layout_row="14" />
<Space
android:layout_width="111dp"
android:layout_height="1dp"
android:layout_column="1"
android:layout_row="0" />
<Space
android:layout_width="77dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_column="2"
android:layout_row="4" />
<Space
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_column="2"
android:layout_row="4" />
<Space
android:layout_width="100dp"
android:layout_height="128dp"
android:layout_column="2"
android:layout_row="4" />
<Space
android:layout_width="100dp"
android:layout_height="162dp"
android:layout_column="2"
android:layout_row="4" />
<Space
android:layout_width="100dp"
android:layout_height="196dp"
android:layout_row="4" />
<TextView
android:id="#+id/tvResult"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="16"
android:text="result" />
<TextView
android:id="#+id/DispAll"
android:layout_height="wrap_content"
android:layout_column="6"
android:layout_gravity="right|top"
android:layout_row="5"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/BtnReturn"
android:layout_column="1"
android:layout_gravity="left|center_vertical"
android:layout_row="17"
android:text="Return To Home Screen" />
<TextView
android:id="#+id/UserDisp"
android:layout_column="2"
android:layout_gravity="right|center_vertical"
android:layout_row="4"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
It would be very helpful and much appreciated if you can help my solve this problem also another issue is whenever i am on the GuessingGame and attempt to start my app crashes and this is the LogCat Error window from it:
03-15 14:48:56.350: E/AndroidRuntime(1397): FATAL EXCEPTION: main
03-15 14:48:56.350: E/AndroidRuntime(1397): Process: com.Lewis.c3383583task3, PID: 1397
03-15 14:48:56.350: E/AndroidRuntime(1397): java.lang.IllegalStateException: Could not execute method of the activity
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.view.View$1.onClick(View.java:3823)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.view.View.performClick(View.java:4438)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.view.View$PerformClick.run(View.java:18422)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.os.Handler.handleCallback(Handler.java:733)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.os.Handler.dispatchMessage(Handler.java:95)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.os.Looper.loop(Looper.java:136)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 14:48:56.350: E/AndroidRuntime(1397): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:48:56.350: E/AndroidRuntime(1397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 14:48:56.350: E/AndroidRuntime(1397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 14:48:56.350: E/AndroidRuntime(1397): at dalvik.system.NativeStart.main(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397): Caused by: java.lang.reflect.InvocationTargetException
03-15 14:48:56.350: E/AndroidRuntime(1397): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 14:48:56.350: E/AndroidRuntime(1397): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 14:48:56.350: E/AndroidRuntime(1397): at android.view.View$1.onClick(View.java:3818)
03-15 14:48:56.350: E/AndroidRuntime(1397): ... 11 more
03-15 14:48:56.350: E/AndroidRuntime(1397): Caused by: java.lang.NullPointerException
03-15 14:48:56.350: E/AndroidRuntime(1397): at com.Lewis.c3383583task3.GuessGame.Start(GuessGame.java:181)
03-15 14:48:56.350: E/AndroidRuntime(1397): ... 14 more
if you see anything that you think could be an issue please tell me thanks.
I have two activities Main, and Game_Activity
When my app launches and I click a button to take me to the Game_Activity it crashes...
Main
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHelp = (Button) findViewById(R.id.btnHelp);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnStart.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, GameActivity.class); //gets us to Game activity when Start Button is clicked
startActivity(i);
overridePendingTransition(R.animator.animation1, R.animator.animation2);
}
});
}
Game_Activity
ViewGroup kamilsLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
Button kamilsButton2 = (Button) findViewById(R.id.kamilsButton2);
kamilsButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
moveButton();
}
});
}
public void moveButton(){
View kamilsButton = findViewById(R.id.kamilsButton);
TransitionManager.beginDelayedTransition(kamilsLayout);
//Change size of the button
ViewGroup.LayoutParams sizeRules = kamilsButton.getLayoutParams();
sizeRules.height = 700;
kamilsButton.setLayoutParams(sizeRules);
}
Here is Logcat sorry for last lines but couldn't format it... :/
03-02 21:32:49.073 22427-22427/com.example.kamil.mychemicalreaction E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kamil.mychemicalreaction, PID: 22427
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamil.mychemicalreaction/com.example.kamil.mychemicalreaction.GameActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kamil.mychemicalreaction.GameActivity.onCreate(GameActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)at android.app.ActivityThread.access$900(ActivityThread.java:161)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:126)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:157)at android.app.ActivityThread.main(ActivityThread.java:5356)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)at dalvik.system.NativeStart.main(Native Method)
XML
Game_Activity
<Button
android:layout_width="150dp"
android:layout_height="1dp"
android:id="#+id/kamilsButton"
android:gravity="bottom|fill_vertical"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff6aff01" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Click"
android:id="#+id/kamilsButton2"
android:textSize="9dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp" />
Menu
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Chemical Reaction"
android:id="#+id/txtTitle"
android:textStyle="bold"
android:textColor="#ffa946ff"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btnStart"
android:text="Start"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginBottom="61dp"
android:layout_above="#+id/btnSettings"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings" />
<Button
android:id="#+id/btnSettings"
android:text="Settings"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_above="#+id/btnHelp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp" />
<Button
android:id="#+id/btnHelp"
android:text="Help"
android:textColor="#ffffffff"
android:textSize="30sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/btnSettings"
android:layout_alignStart="#+id/btnSettings"
android:layout_marginBottom="81dp" />
You're getting a NullPointerException when trying to reference your UI elements. From your XML, it appears you haven't given any layout an id of kamilsLayout. Try enclosing your UI elements in a layout with this ID.
Like Josef said. You're setting the same XML for two both activities, is that expected?
You're using setContentView(R.layout.activity_main). Maybe there is no ViewGroup on R.layout.activity_main layout. And you're trying to setting up that `
kamilsLayout = (ViewGroup) findViewById(R.id.kamilsLayout);
`
I run my app on the emulator and when I click on the button for Clubs it just crashes and gives the message "Unfortunately app has stopped in Android Emulator". I've looked at the logcat and it says to check its declared in the manifest file which it is. So I'm just not sure what do. I'll put up my code for the layout file, the java file and the manifest file. Any help would be greatly appreciated.
<?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:background="#drawable/background"
android:orientation="vertical" >
<ImageView
android:id="#+id/school_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:contentDescription="#string/logo_desc"
android:src="#drawable/logo" />
<TextView
android:id="#+id/title_socities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="20dp"
android:textSize="20sp"
android:textColor="#CC0000"
android:text="#string/socities_info" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/ace"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/ace_icon"
android:padding="20dp"
android:text="Ace Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/ace_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ace" />
<ImageButton
android:id="#+id/ace_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/ace"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/anime_manga"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/anime_manga_icon"
android:padding="20dp"
android:text="Anime Manga"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/anime_manga_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/anime_manga" />
<ImageButton
android:id="#+id/anime_manga_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/anime_manga"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/best_buddies"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/best_buddies_icon"
android:padding="20dp"
android:text="Best Buddies"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/best_buddies_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/best_buddies" />
<ImageButton
android:id="#+id/best_buddies_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/best_buddies"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/computer_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/computer_society_icon"
android:padding="20dp"
android:text="Computer Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/computer_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/computer_socity" />
<ImageButton
android:id="#+id/computer_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_toRightOf="#+id/computer_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/cumann_gaelach"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/cumann_gaelach_icon"
android:padding="20dp"
android:text="Cumann Gaelach"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/cumann_gaelach_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/cumann_gaelach" />
<ImageButton
android:id="#+id/cumann_gaelach_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/cumann_gaelach"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/dj_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/dj_society_icon"
android:padding="20dp"
android:text="DJ Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/dj_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/dj_society" />
<ImageButton
android:id="#+id/dj_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/dj_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/eng_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/eng_society_icon"
android:padding="20dp"
android:text="Engineering Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/eng_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/eng_society" />
<ImageButton
android:id="#+id/eng_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/eng_society"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/equestrain"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/equestrain_icon"
android:padding="20dp"
android:text="Equestrain"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/equestrain_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/equestrian" />
<ImageButton
android:id="#+id/equestrain_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/equestrain"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/fianna_fail"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/fianna_fail_icon"
android:padding="20dp"
android:text="Fianna Fail"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/fianna_fail_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/fianna_fail" />
<ImageButton
android:id="#+id/fianna_fail_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/fianna_fail"
android:src="#drawable/facebook" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/film_society"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="false"
android:layout_toRightOf="#+id/film_society_icon"
android:padding="20dp"
android:text="Film Society"
android:textColor="#CC0000"
android:textSize="20sp" />
<ImageView
android:id="#+id/film_society_icon"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/film_society" />
<ImageButton
android:id="#+id/film_society_fb"
android:layout_width="206dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/film_society"
android:src="#drawable/facebook" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Java file
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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;
public class Clubs extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.socities);
Button aces = (Button) findViewById(R.id.ace_fb);
Button anime_manga = (Button) findViewById(R.id.anime_manga_fb);
Button best_buddies = (Button) findViewById(R.id.best_buddies_fb);
Button computer_society = (Button) findViewById(R.id.computer_society_fb);
Button cumann_gaelach = (Button) findViewById(R.id.cumann_gaelach_fb);
Button dj_society = (Button) findViewById(R.id.dj_society_fb);
Button eng_society = (Button) findViewById(R.id.eng_society_fb);
Button equestrian = (Button) findViewById(R.id.equestrain_fb);
Button fianna_fail = (Button) findViewById(R.id.fianna_fail_fb);
Button film_society = (Button) findViewById(R.id.film_society_fb);
Button foiroige = (Button) findViewById(R.id.foiroige_fb);
Button games = (Button) findViewById(R.id.games_fb);
Button wish_makers_on_campus = (Button) findViewById(R.id.wish_makers_on_campus_fb);
Button heritage = (Button) findViewById(R.id.heritage_fb);
Button internationl_society = (Button) findViewById(R.id.internationl_society_fb);
Button letterfrack = (Button) findViewById(R.id.letterfrack_fb);
Button lgbt = (Button) findViewById(R.id.lgbt_fb);
Button living_history = (Button) findViewById(R.id.living_history_fb);
Button mature_students = (Button) findViewById(R.id.mature_students_fb);
Button photography = (Button) findViewById(R.id.photography_fb);
Button robotics = (Button) findViewById(R.id.robotics_fb);
Button science = (Button) findViewById(R.id.science_fb);
Button sinnfein = (Button) findViewById(R.id.sinnfein_fb);
aces.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/aceofclubsgmit?fref=ts"));
startActivity(myWebLink);
}
});
anime_manga.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmitanimemangasoc"));
startActivity(myWebLink);
}
});
best_buddies.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/Best-Buddies-GMIT/305305566158422?fref=ts"));
startActivity(myWebLink);
}
});
computer_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/GMITComputerSociety"));
startActivity(myWebLink);
}
});
cumann_gaelach.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmitcumannnagaeilge?fref=ts"));
startActivity(myWebLink);
}
});
dj_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/GmitDjSociety"));
startActivity(myWebLink);
}
});
eng_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/EngSoc-GMIT/196642613742557"));
startActivity(myWebLink);
}
});
equestrian.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/gmit.ridingclub"));
startActivity(myWebLink);
}
});
fianna_fail.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/sean.mulroycumann"));
startActivity(myWebLink);
}
});
film_society.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/GMIT-Film-Making-Society/1376699195883960?fref=ts"));
startActivity(myWebLink);
}
});
}
#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);
}
}
============================================================================
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ie.gmit.pleasework"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="ie.gmit.pleasework.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ie.gmit.pleasework.Sponsors"
android:label="#string/airsoft" >
</activity>
<activity
android:name="ie.gmit.pleasework.Societies"
android:label="#string/airsoft" >
</activity>
<activity
android:name="ie.gmit.pleasework.Clubs"
android:label="#string/airsoft" >
</activity>
</application>
</manifest>
=================================================================================
logcat
04-29 11:50:28.660: E/AndroidRuntime(1642): FATAL EXCEPTION: main
04-29 11:50:28.660: E/AndroidRuntime(1642): Process: ie.gmit.pleasework, PID: 1642
04-29 11:50:28.660: E/AndroidRuntime(1642): android.content.ActivityNotFoundException: Unable to find explicit activity class {ie.gmit.pleasework/ie.gmit.pleasework.Societies}; have you declared this activity in your AndroidManifest.xml?
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivityForResult(Activity.java:3424)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivityForResult(Activity.java:3385)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivity(Activity.java:3627)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.Activity.startActivity(Activity.java:3595)
04-29 11:50:28.660: E/AndroidRuntime(1642): at ie.gmit.pleasework.MainActivity$8.onClick(MainActivity.java:101)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.view.View.performClick(View.java:4438)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.view.View$PerformClick.run(View.java:18422)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Handler.handleCallback(Handler.java:733)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.os.Looper.loop(Looper.java:136)
04-29 11:50:28.660: E/AndroidRuntime(1642): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-29 11:50:28.660: E/AndroidRuntime(1642): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 11:50:28.660: E/AndroidRuntime(1642): at java.lang.reflect.Method.invoke(Method.java:515)
04-29 11:50:28.660: E/AndroidRuntime(1642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-29 11:50:28.660: E/AndroidRuntime(1642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-29 11:50:28.660: E/AndroidRuntime(1642): at dalvik.system.NativeStart.main(Native Method)
Main Activity
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button contact = (Button) findViewById(R.id.btnContact);
Button fixturesAndResults = (Button) findViewById(R.id.btnFixtureandResults);
Button newsFeed = (Button) findViewById(R.id.btnNewsFeed);
Button sponsors = (Button) findViewById(R.id.btnSponsors);
Button scholarships = (Button) findViewById(R.id.btnScholarships);
Button facilites = (Button) findViewById(R.id.btnFacilities);
Button clubs = (Button) findViewById(R.id.btnClubs);
Button societies = (Button) findViewById(R.id.btnSocieties);
Button about = (Button) findViewById(R.id.btnAbout);
contact.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Contact.class);
startActivity(i);
}
});
fixturesAndResults.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), FixturesAndResults.class);
startActivity(i);
}
});
newsFeed.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), NewsFeed.class);
startActivity(i);
}
});
sponsors.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Sponsors.class);
startActivity(i);
}
});
scholarships.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Scholarships.class);
startActivity(i);
}
});
facilites.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Facilites.class);
startActivity(i);
}
});
clubs.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Clubs.class);
startActivity(i);
}
});
societies.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), Societies.class);
startActivity(i);
}
});
about.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(), About.class);
startActivity(i);
}
});
}
}
You declared your Activities incorrectly in your manifest.
You wrote, for instance
<activity
android:name="ie.gmit.pleasework.Societies"
android:label="#string/airsoft" >
</activity>
and should have put
<activity
android:name=".Societies"
android:label="#string/airsoft" >
</activity>
The names of the classes are taken to be from the root of your application (ie.gmit.pleasework). By writing your complete package twice, you're asking Dalvik to find the class ie.gmit.pleasework/ie.gmit.pleasework.Societies
I'm beginner in android developement. And i have a problem with my code. The application is totally crashed. It is Java error in main.
MainActivity.java:
package hu.cgkni6.uniobuda.testapplication;
import hu.cgkni6.uniobuda.testapplication.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
final String username = "diak";
final String pass = "diak";
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (usrname.getText().toString().equals(username))
{
if (passwd.getText().toString().equals(pass))
{
String text2 = "Success!";
Toast.makeText(MainActivity.this, text2, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Invalid Password", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Invalid username", Toast.LENGTH_LONG).show();
}
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#45B7FA" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:text="#string/login_interface" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/username" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/password" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_OK"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/ok" />
<Button
android:id="#+id/btn_Cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="#string/cancel" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/about_us"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/contact_us" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="#string/help" />
</TableRow>
</RelativeLayout>
</LinearLayout>
Catlog:
10-09 09:18:26.244: E/AndroidRuntime(302): FATAL EXCEPTION: main
10-09 09:18:26.244: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.cgkni6.uniobuda.testapplication/hu.cgkni6.uniobuda.testapplication.MainActivity}: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 09:18:26.244: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Activity.findViewById(Activity.java:1637)
10-09 09:18:26.244: E/AndroidRuntime(302): at hu.cgkni6.uniobuda.testapplication.MainActivity.<init>(MainActivity.java:17)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstanceImpl(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstance(Class.java:1429)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-09 09:18:26.244: E/AndroidRuntime(302): ... 11 more
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
Put this in the oncreate method after setContentView(R.layout.activity_main);
and you should be good. You cant do it from that location :)
You should initialize your Views after setting the layout in your onCreate() method.
You are getting the error because you are trying to access the views before setting the layout that is why the application is not able to find the view's id.
Change your code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
You're trying to find views before your layout is setup. Move
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
to your onCreate method. Just after your setContentView call.
First of all you should learn Android life cycle
you should find your all button and edit text in the oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
}
Initialize you Button and EditText outside the onCreate method like this
Button okbutton;
Button cancelbutton;
EditText usrname;
EditText passwd;
Then insert this
okbutton = (Button) findViewById(R.id.btn_OK);
cancelbutton = (Button) findViewById(R.id.btn_Cancel);
EditText usrname = (EditText) findViewById(R.id.editText1);
EditText passwd = (EditText) findViewById(R.id.editText2);
inside your onCreate method after the setContentView(R.layout.activity_main);
Hope it helps.
The Main thing happens in FATAL EXECEPTION my App don't Launches and error comes unfortunately stopped it keeps crashing
Tried switch case and else if still the error continues the app keeps crashing
Googled and search over stackflow the different usersover stackflowed faced this error still the error continues with me even after trying their solution with single if and single onclicklistener app manages to run but not with multiple onlicklistener is there a different way to run multiple onclick listener app please help me
here's MainActivity.java
package com.example.cricscore;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private int mCount = 0;
private static final String TAG = "MainActivity";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) this);
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.button1:
mCount++;
final TextView countTextView = (TextView) findViewById(R.id.textView1);
countTextView.setText(""+ mCount);
break;
case R.id.button2:
mCount= mCount+2 ;
final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
countTextView1.setText(""+ mCount);
break;
}
}
catch(Throwable t) {
Log.e(TAG, t.getMessage(), t);
}
}
}
And the Activity_main.xml is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/button2"
android:layout_below="#+id/textView1"
android:text="Runs" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/textView2"
android:layout_marginRight="19dp"
android:text="Wickets" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView4"
android:layout_alignParentRight="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text="1" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:text="2" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:text="6" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button5"
android:layout_alignRight="#+id/textView4"
android:text="Wicket" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button8"
android:layout_alignBottom="#+id/button8"
android:layout_alignLeft="#+id/button5"
android:text="Balls" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_toRightOf="#+id/button7"
android:text="4" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="44dp"
android:layout_toLeftOf="#+id/textView2"
android:text="Button" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:layout_alignBottom="#+id/button4"
android:layout_toLeftOf="#+id/textView7"
android:text="No/Wide" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView7"
android:layout_alignBottom="#+id/textView7"
android:layout_alignRight="#+id/button5"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button8"
android:layout_toLeftOf="#+id/textView4"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</RelativeLayout>
Logcat
03-18 14:09:37.717: E/AndroidRuntime(360): FATAL EXCEPTION: main
03-18 14:09:37.717: E/AndroidRuntime(360): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.abc/com.example.abc.MainActivity}: java.lang.ClassCastException:
com.example.abc.MainActivity
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1663)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread.access$1500
(ActivityThread.java:117)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:931)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.os.Handler.dispatchMessage
(Handler.java:99)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.os.Looper.loop(Looper.java:123)
03-18 14:09:37.717: E/AndroidRuntime(360): at android.app.ActivityThread.main
(ActivityThread.java:3683)
03-18 14:09:37.717: E/AndroidRuntime(360): at java.lang.reflect.Method.invokeNative(Native
Method)
03-18 14:09:37.717: E/AndroidRuntime(360): at java.lang.reflect.Method.invoke
(Method.java:507)
03-18 14:09:37.717: E/AndroidRuntime(360): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 14:09:37.717: E/AndroidRuntime(360): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:597)
03-18 14:09:37.717: E/AndroidRuntime(360): at dalvik.system.NativeStart.main(Native Method)
03-18 14:09:37.717: E/AndroidRuntime(360): Caused by: java.lang.ClassCastException:
com.example.abc.MainActivity
03-18 14:09:37.717: E/AndroidRuntime(360): at com.example.abc.MainActivity.onCreate
(MainActivity.java:30)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 14:09:37.717: E/AndroidRuntime(360): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-18 14:09:37.717: E/AndroidRuntime(360): ... 11 more
After Some Modification As told by the 1st answer my code didn't crashed but I was only able to see blank activity where android_mainfest is still the same
package com.example.cricscore;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
// Private member field to keep track of the count
private int mCount = 0;
/** Called when the activity is first created.
* #param OnClickListener */
public void onCreate(Bundle savedInstanceState, android.view.View.OnClickListener
OnClickListener){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) );
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) );
}
public void OnClickListener(View v) {
switch (v.getId()) {
case R.id.button1:
mCount++;
final TextView countTextView = (TextView) findViewById(R.id.textView1);
countTextView.setText(""+ mCount);
break;
case R.id.button2:
mCount= mCount+2 ;
final TextView countTextView1 = (TextView) findViewById(R.id.textView1);
countTextView1.setText(""+ mCount);
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The setOnClickListener method expects a parameter of type View.OnClickListener. What you are doing is taking the Activity itself, and trying to coerce it being a OnClickListener which it is not, in this piece of code:
Button buttonA = (Button) findViewById(R.id.button1);
buttonA.setOnClickListener((OnClickListener) this); // <-- here
Button buttonB = (Button) findViewById(R.id.button2);
buttonB.setOnClickListener((OnClickListener) this); // <-- and here too
You should declare your activity to be an OnClickListener, using:
public class MainActivity extends Activity implements OnClickListener
and as you already have a onClick(View v) method, the problem should be fixed.