Apps SOLVED: App help

brggames

Newbie
Howdy. Just getting into android programming and have ran into a snag. My program opens with a splash screen then goes to a menu screen with 3 buttons. The 2 buttons I've coded so far work, but the target class of one crashes. I know the buttons work because I had it go to a temp class I made and it went there fine.

Also, when I only have the basic code in the target class it works.

package com.brggames.dquest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class temp extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.hall1);
}

}
That works, but the next one does not work. It crashes.

package com.brggames.dquest;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class hallOne extends Activity {

MediaPlayer bclick1;
Button bForward1, bBack1, bLeft1, bRight1, Bpause1, bPlayMusic1;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.hall1);


bclick1 = MediaPlayer.create(this, R.raw.bclick1);

Button bLeft1 = (Button) findViewById(R.id.bLeft1);
bLeft1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

Button bRight1 = (Button) findViewById(R.id.bRight1);
bRight1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

Button bForward1 = (Button) findViewById(R.id.bForward1);
bForward1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

Button bBack1 = (Button) findViewById(R.id.bBack1);
bBack1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

Button bPause1 = (Button) findViewById(R.id.bPause1);
bPause1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

Button bPlayMusic1 = (Button) findViewById(R.id.bPlayMusic1);
bPlayMusic1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

}

}
and here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView android:layout_width="fill_parent"
android:id="@+id/imageView1"
android:layout_height="fill_parent"
android:src="@drawable/hall1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>

<Button android:id="@+id/bPause1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Pause"
android:layout_alignTop="@+id/bPlayMusic1"
android:textStyle="bold"/>

<Button android:id="@+id/bBack1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="B"
android:layout_width="48dp"
android:layout_height="48dp"
android:textSize="24dp"
android:textStyle="bold"/>

<Button android:id="@+id/bForward1"
android:layout_above="@+id/bRight1"
android:layout_alignLeft="@+id/bBack1"
android:layout_centerHorizontal="true"
android:text="F"
android:layout_width="48dp"
android:layout_height="48dp"
android:textSize="24dp"
android:textStyle="bold"/>

<Button android:id="@+id/bRight1"
android:layout_above="@+id/bBack1"
android:layout_toRightOf="@+id/bBack1"
android:text="R"
android:layout_width="48dp"
android:layout_height="48dp"
android:textSize="24dp"
android:textStyle="bold"
android:focusableInTouchMode="false"
android:focusable="false"/>

<Button android:id="@+id/bLeft1"
android:layout_above="@+id/bBack1"
android:layout_toLeftOf="@+id/bBack1"
android:text="L"
android:layout_width="48dp"
android:layout_height="48dp"
android:textSize="24dp"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"/>

<ImageButton android:layout_height="wrap_content"
android:src="@drawable/play"
android:layout_width="wrap_content"
android:id="@+id/bPlayMusic1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>

</RelativeLayout>
Any help would be appreciated.
 

mills2533

Well-Known Member
Remove "Button" from in front of each of the lines like this:
Button bLeft1 = (Button) findViewById(R.id.bLeft1);

Make it this instead:
bLeft1 = (Button) findViewById(R.id.bLeft1);

Because you've already declared the buttons here:
Button bForward1, bBack1, bLeft1, bRight1, Bpause1, bPlayMusic1;
 

brggames

Newbie
Thread starter
Still crashes, here is the modified code

package com.brggames.dquest;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Button;

public class hallOne extends Activity {


MediaPlayer bclick1;
Button bForward1, bBack1, bLeft1, bRight1, bPause1, bPlayMusic1;
int rNumberB;
int rNumberF;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.hall1);


bclick1 = MediaPlayer.create(this, R.raw.bclick1);

bLeft1 = (Button) findViewById(R.id.bLeft1);
bLeft1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bRight1 = (Button) findViewById(R.id.bRight1);
bRight1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bForward1 = (Button) findViewById(R.id.bForward1);
bForward1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();

Thread playgameF = new Thread(){
public void run(){


try{
// get a random number from 1 to 8
rNumberF = (int) Math.floor(Math.random()* 8 + 1);


//Switch Case decides which random hall to go to
switch (rNumberF){
case 1:
//go to hall 1
startActivity (new Intent("com.brggames.dquest.HALLONE"));
break;

case 2:
// go to hall 2
startActivity (new Intent("com.brggames.dquest.HALLTWO"));
break;

case 3:
// go to hall 3
startActivity (new Intent("com.brggames.dquest.HALLTHREE"));
break;

case 4:
// go to hall 4
startActivity (new Intent("com.brggames.dquest.HALLFOUR"));
break;

case 5:
// go to hall 5
startActivity (new Intent("com.brggames.dquest.HALLFIVE"));
break;

case 6:
// go to hall 6
startActivity (new Intent("com.brggames.dquest.HALLSIX"));
break;

case 7:
// go to hall 7
startActivity (new Intent("com.brggames.dquest.HALLSEVEN"));
break;

case 8:
// go to hall 8
startActivity (new Intent("com.brggames.dquest.HALLEIGHT"));
break;


}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}

};
playgameF.start();
}
});

bBack1 = (Button) findViewById(R.id.bBack1);
bBack1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();

Thread playgameB = new Thread(){
public void run(){

try{
// get a random number from 1 to 8
rNumberB = (int) Math.floor(Math.random()* 8 + 1);


//Switch Case decides which random hall to go to
switch (rNumberB){
case 1:
//go to hall 1
startActivity (new Intent("com.brggames.dquest.HALLONE"));
break;

case 2:
// go to hall 2
startActivity (new Intent("com.brggames.dquest.HALLTWO"));
break;

case 3:
// go to hall 3
startActivity (new Intent("com.brggames.dquest.HALLTHREE"));
break;

case 4:
// go to hall 4
startActivity (new Intent("com.brggames.dquest.HALLFOUR"));
break;

case 5:
// go to hall 5
startActivity (new Intent("com.brggames.dquest.HALLFIVE"));
break;

case 6:
// go to hall 6
startActivity (new Intent("com.brggames.dquest.HALLSIX"));
break;

case 7:
// go to hall 7
startActivity (new Intent("com.brggames.dquest.HALLSEVEN"));
break;

case 8:
// go to hall 8
startActivity (new Intent("com.brggames.dquest.HALLEIGHT"));
break;


}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}

};
playgameB.start();
}
});

bPause1 = (Button) findViewById(R.id.bPause1);
bPause1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bPlayMusic1 = (Button) findViewById(R.id.bPlayMusic1);
bPlayMusic1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

}
//Menu code below
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater miMenu = getMenuInflater();
miMenu.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.restart:
startActivity (new Intent("com.brggames.dquest.RESTART"));
return true;
case R.id.options:
startActivity (new Intent("com.brggames.dquest.OPTIONS"));
return true;
case R.id.quit:
finish();
return true;
}
return false;

}
}
 

brggames

Newbie
Thread starter
I comment out everything and it works. I doubled and tripled checked my AndroidManifest.xml, and for the life of my I can't figure it out. As I said, I'm new and may be missing something very simple. Here is the code with the stuff commented out. the menu code at the bottom works perfectly (the restar button isn't coded yet).

package com.brggames.dquest;

import android.app.Activity;
import android.content.Intent;
//import android.media.MediaPlayer;
import android.os.Bundle;
//import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
//import android.widget.Button;

public class hallOne extends Activity {

/*
MediaPlayer bclick1;
Button bForward1, bBack1, bLeft1, bRight1, bPause1, bPlayMusic1;
int rNumberB;
int rNumberF;
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.hall1);

/*
bclick1 = MediaPlayer.create(this, R.raw.bclick1);

bLeft1 = (Button) findViewById(R.id.bLeft1);
bLeft1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bRight1 = (Button) findViewById(R.id.bRight1);
bRight1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bForward1 = (Button) findViewById(R.id.bForward1);
bForward1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();


Thread playgameF = new Thread(){
public void run(){


try{
// get a random number from 1 to 8
rNumberF = (int) Math.floor(Math.random()* 8 + 1);


//Switch Case decides which random hall to go to
switch (rNumberF){
case 1:
//go to hall 1
startActivity (new Intent("com.brggames.dquest.HALLONE"));
break;

case 2:
// go to hall 2
startActivity (new Intent("com.brggames.dquest.HALLTWO"));
break;

case 3:
// go to hall 3
startActivity (new Intent("com.brggames.dquest.HALLTHREE"));
break;

case 4:
// go to hall 4
startActivity (new Intent("com.brggames.dquest.HALLFOUR"));
break;

case 5:
// go to hall 5
startActivity (new Intent("com.brggames.dquest.HALLFIVE"));
break;

case 6:
// go to hall 6
startActivity (new Intent("com.brggames.dquest.HALLSIX"));
break;

case 7:
// go to hall 7
startActivity (new Intent("com.brggames.dquest.HALLSEVEN"));
break;

case 8:
// go to hall 8
startActivity (new Intent("com.brggames.dquest.HALLEIGHT"));
break;


}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}

};
playgameF.start();

}
});

bBack1 = (Button) findViewById(R.id.bBack1);
bBack1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();

/*
Thread playgameB = new Thread(){
public void run(){

try{
// get a random number from 1 to 8
rNumberB = (int) Math.floor(Math.random()* 8 + 1);


//Switch Case decides which random hall to go to
switch (rNumberB){
case 1:
//go to hall 1
startActivity (new Intent("com.brggames.dquest.HALLONE"));
break;

case 2:
// go to hall 2
startActivity (new Intent("com.brggames.dquest.HALLTWO"));
break;

case 3:
// go to hall 3
startActivity (new Intent("com.brggames.dquest.HALLTHREE"));
break;

case 4:
// go to hall 4
startActivity (new Intent("com.brggames.dquest.HALLFOUR"));
break;

case 5:
// go to hall 5
startActivity (new Intent("com.brggames.dquest.HALLFIVE"));
break;

case 6:
// go to hall 6
startActivity (new Intent("com.brggames.dquest.HALLSIX"));
break;

case 7:
// go to hall 7
startActivity (new Intent("com.brggames.dquest.HALLSEVEN"));
break;

case 8:
// go to hall 8
startActivity (new Intent("com.brggames.dquest.HALLEIGHT"));
break;


}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}

};
playgameB.start();

}
});

bPause1 = (Button) findViewById(R.id.bPause1);
bPause1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});

bPlayMusic1 = (Button) findViewById(R.id.bPlayMusic1);
bPlayMusic1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bclick1.start();
}
});
*/
}


//Menu code below
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater miMenu = getMenuInflater();
miMenu.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.restart:
startActivity (new Intent("com.brggames.dquest.RESTART"));
return true;
case R.id.options:
startActivity (new Intent("com.brggames.dquest.OPTIONS"));
return true;
case R.id.quit:
finish();
return true;
}
return false;

}
}
 

brggames

Newbie
Thread starter
here is the start activity code in the previous class

startActivity (new Intent("com.brggames.dquest.HALLONE"));

and the activity code in the AndroidManifest.xml

<activity android:name=".hallOne"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.brggames.dquest.HALLONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
 
Top