2 Replies - 674 Views - Last Post: 29 December 2012 - 05:07 PM

#1 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Activity not Starting

Posted 29 December 2012 - 04:34 PM

Hi, my activity is not starting right now. Seems like I'm doing every thing right. It's defined in the manifest and every thing. It may have to do with the "Draw" class, possibly?


Here's the code for the main activity:

package com.example.gameproject;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;

public class MainMenu extends Activity implements onclickListener  {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);
		Button startGame = (Button)findViewById(R.id.startGame);
		startGame.setonclickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);
		return true;
	}
	
	void startGame(View v){
		Intent startGame = new Intent(this, StartGameActivity.class);
		startActivity(startGame);
		
		
	}

	@Override
	public void onclick(View v) {
		// TODO Auto-generated method stub
		
	}

	
	

}



Here's the layout xml for that 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"
    tools:context=".MainMenu" >

    <Button
        android:id="@+id/startGame"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp"
        android:onclick="startGame"
        android:text="@string/startGame" />

    <Button
        android:id="@+id/exit"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/highScores"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="Exit" />

    <Button
        android:id="@+id/highScores"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/startGame"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:onclick="startScores"
        android:text="@string/highScores" />

</RelativeLayout>



here's the start game activity:
package com.example.gameproject;

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

public class StartGameActivity extends Activity {
	Draw view;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(view);
	}

	


}


here's my draw class that provides that startGameActivity view:
package com.example.gameproject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Draw extends View {
	Paint paint;
	public Draw(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		paint.setColor(Color.BLACK);
		canvas.drawColor(Color.CYAN);
		//canvas.drawText("5.5", canvas.getWidth()/2, canvas.getHeight()/2, paint);
	}

}



The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gameproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gameproject.MainMenu"
            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=".StartGameActivity"
            android:label="This is the name of your game"
            />
    </application>

</manifest>


And finally, the logcat:
12-29 18:33:27.785: V/InputMethodManager(13441): Has been inactive!  Starting fresh
12-29 18:33:27.785: V/InputMethodManager(13441): Starting input: tba=android.view.inputmethod.EditorInfo@41528a78 ic=null



Not much to the logcat

Is This A Good Question/Topic? 0
  • +

Replies To: Activity not Starting

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Activity not Starting

Posted 29 December 2012 - 04:53 PM

In StartGameActivity you need to create a new instance of Draw!
package com.example.gameproject;

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

public class StartGameActivity extends Activity {
	Draw view;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		view = new Draw();// create a new instance of Draw
		setContentView(view);
	}

	


}




Also it does not look like you are replacing the onclick listener deffinition from your layout file with the implementation in your main activity.
This is how it should be done:
package com.example.gameproject;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;

public class MainMenu extends Activity implements onclickListener  {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);
		Button startGame = (Button)findViewById(R.id.startGame);
		startGame.setonclickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);
		return true;
	}
	
	void startGame(){// remove View v
		Intent startGame = new Intent(this, StartGameActivity.class);
		startActivity(startGame);
		
		
	}

	@Override
	public void onclick(View v) {
		switch(v.getId())// get the id of the view
		{
			case R.id.startGame: // if it matches the id of the game button it will start game
			{
				startGame();
				break;
			}
		}
	}

}



Now in your xml layout for the activity remove android:onclick="startGame" from it.
Was This Post Helpful? 2
  • +
  • -

#3 recheej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 26-December 12

Re: Activity not Starting

Posted 29 December 2012 - 05:07 PM

View PostH3R3T1C, on 29 December 2012 - 04:53 PM, said:

Now in your xml layout for the activity remove android:onclick="startGame" from it.

Thank you. That was killing me. But I just left the start game method as it is and took out the onclicklistener implementation.

public class MainMenu extends Activity   {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);
		return true;
	}
	
	public void startGame(View v){
		Intent startGame = new Intent(this, StartGameActivity.class);
		startActivity(startGame);
		
		
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1