Treasure hunt has stopped working - android

When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.
Main Activity:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void Start(View view) {
Log.d("hi", "hello");
Intent intent = new Intent(this, Level_1.class);
startActivity(intent);
finish();
}
}
Mainxml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Treasure Hunt" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:text="Start"
android:onClick="Start" />
</RelativeLayout>
`
level_1java:
package com.example.treasurehunt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class Level_1 extends Activity {
private SharedPreferences settings;
private SharedPreferences.Editor editor;
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
String hint;
String answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.level_1, menu);
return true;
}
public void level1() {
editor.putInt("level", 1); // only needs to be done for level 1
editor.commit();
Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?");
hint = "July IV MDCCLXXVI";
answer = "statue of liberty";
}
public void answer() {
String useranswer = edittext.toString();
if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer
int leveltest = settings.getInt("level", 1);
editor.putInt("level", leveltest+1); //adds one level to the current level
editor.commit();
}
}
public void hint() { //function that displays the hint
}
}
level_1 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Level_1" >
<TextView
android:id="#+id/Clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Clue" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Clue"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editText"
android:layout_marginBottom="50dp"
android:text="Submit"
android:onClick="answer"/>
<Button
android:id="#+id/Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Submit"
android:layout_alignBottom="#+id/Submit"
android:layout_alignLeft="#+id/editText"
android:text="Hint"
android:onClick="Hint"/>
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.treasurehunt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.treasurehunt.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="com.example.treasurehunt.Level_1"
android:label="#string/title_activity_level_1" >
</activity>
</application>
</manifest>

Seems you're having NullPointerException.
On the field declaration
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
Move the assignments to in onCreate()
public class Level_1 extends Activity
{
// Other members ....
TextView Clue = (TextView)findViewById(R.id.Clue);
EditText edittext = (EditText)findViewById(R.id.editText);
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("hi","hello");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_1);
// Assignemnts should be here
Clue = (TextView)findViewById(R.id.Clue);
edittext = (EditText)findViewById(R.id.editText);
int level = settings.getInt("level", 1);
switch (level) {
case 1: level1();
break;
default:
break;
}
}
}

Related

I have an error opening trace file and a unable to start activity compotentInfo and no fix seems to work, Android

Error(s):
10-17 18:53:39.298: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quizme/com.quizme.QuizMeActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView
10-17 18:53:38.747: error opening trace file: No such file or directory (2)
Upon trying to run it I get the unfortunatly, the app does not work error as well.
Java file:
package com.quizme;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class QuizMeActivity extends Activity {
private TextView question;
private TextView result;
private ImageButton forward;
private ImageButton backward;
private Button bTrue;
private Button bFalse;
private TrueFalse mTrueFalse;
/*
* onCreate(Bundle savedInstanceState)
* Instantiates all private GUI elements to their corresponding Views
* in activity_quiz_me.xml
* Sets the string values of the questions in the TrueFalse object,
* and sets their truth values.
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_me);
Resources res = this.getResources(); //this is to be able to get strings
bTrue=(Button)findViewById(R.id.true_button);
forward=(ImageButton)findViewById(R.id.forward_button);
bFalse=(Button)findViewById(R.id.false_button);
backward=(ImageButton)findViewById(R.id.back_button);
result=(TextView)findViewById(R.id.result);
result.setText("");
question=(TextView)findViewById(R.id.question);
question.setText(mTrueFalse.getmQuestionSet()[0]);
mTrueFalse.setmQuestionSet(0, (String) res.getText(R.string.q1));
mTrueFalse.setmQuestionSet(1, (String) res.getText(R.string.q2));
mTrueFalse.setmQuestionSet(2, (String) res.getText(R.string.q3));
mTrueFalse.setmQuestionSet(3, (String) res.getText(R.string.q4));
mTrueFalse.setmQuestionSet(4, (String) res.getText(R.string.q5));
mTrueFalse.setmArrOfBols(0, false);
mTrueFalse.setmArrOfBols(1, true);
mTrueFalse.setmArrOfBols(2, true);
mTrueFalse.setmArrOfBols(3, false);
mTrueFalse.setmArrOfBols(4, true);
}
public void checkTrue(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.right));
}
else
{
result.setText((String) res.getText(R.string.wrong));
}
}
public void checkFalse(View view) {
Resources res = this.getResources(); //this is to be able to get strings
if(mTrueFalse.getCurrentBolValue()==true)
{
result.setText((String) res.getText(R.string.wrong));
}
else
{
result.setText((String) res.getText(R.string.right));
}
}
public void goForward(View view) {
if(mTrueFalse.getmCurrentQ()<mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()+1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==mTrueFalse.getmQuestionSet().length-1)
{
mTrueFalse.setmCurrentQ(0);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
public void goBack(View view) {
if(mTrueFalse.getmCurrentQ()>0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmCurrentQ()-1);
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
else if(mTrueFalse.getmCurrentQ()==0)
{
mTrueFalse.setmCurrentQ(mTrueFalse.getmQuestionSet().length-1); //for the purposes of the project, its setting currentQ to 4
question.setText((String) (mTrueFalse.getCurrentQuestionStr()));
result.setText("");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_me, 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);
}
}
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"
android:orientation="vertical"
android:id="#+id/activity_view"
tools:context="com.quizme.QuizMeActivity">
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sTrue"
android:layout_marginRight="180dp"
android:onClick="checkTrue"
/>
<ImageButton
android:id="#+id/forward_button"
android:contentDescription="#string/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/back_button"
android:onClick="goForward"
android:src="#drawable/forward" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/question"
android:layout_toRightOf="#+id/true_button"
android:onClick="checkFalse"
android:text="#string/sFalse" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:contentDescription="#string/back"
android:onClick="goBack"
android:src="#drawable/back" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/resultStr"
android:textSize="30sp" />
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/true_button"
android:layout_marginTop="34dp"
android:layout_toLeftOf="#+id/forward_button"
android:text="#string/questionStr"
android:textSize="25sp" />
</RelativeLayout>
Android Manifest:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".QuizMeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
sorry for the pile of code, I am just frustrated and stuck
Try cleaning the project
Eclipse: Project->Clean

How to get the SeekBar work to adjust brightness

First of all, I am new to Android Developing so don't be so harsh on me.
I have tried to get the SeekBar to adjust the brightness level of either the application or the entire system. Although, I have not been able to get this to work.
My XML-file looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SettingsPage" >
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/seekBar"
android:layout_below="#+id/seekBar"
android:layout_marginTop="30dp" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:max="100"
android:progress="50" />
</RelativeLayout>
XML code for MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/buttonSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button"
android:onClick="settingsPage"/>
</RelativeLayout>
XML code, the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testsetup"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testsetup.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="com.example.testsetup.SettingsPage"
android:label="#string/title_activity_settings_page" >
</activity>
<activity
android:name="com.example.testsetup.BrightnessAdjuster"
android:label="#string/title_activity_brightness_adjuster" >
</activity>
<activity
android:name="com.example.testsetup.BrightnessActivity"
android:label="#string/title_activity_brightness" >
</activity>
</application>
</manifest>
My Java-code that I have looks like this:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SettingsPage extends Activity {
float BackLightValue = 0.5f; // dummy default value
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_page);
SeekBar BackLightControl = (SeekBar) findViewById(R.id.action_settings);
BackLightControl
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener());
}
public void onClick(View arg0) {
int SysBackLightValue = (int) (BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
BackLightValue = (float) arg1 / 100;
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
public void onStartTrackingTouch(SeekBar arg0) {
}
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings_page, menu);
return true;
}
}
Java code for MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void settingsPage(View view){
Intent intent = new Intent(this, SettingsPage.class);
startActivity(intent);
}
}
The problem is that I do not know if this is the right way, even though I have tried to follow several guides on the matter.
Also, I get one error in the above code, and it is the following line
BackLightControl
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener());
I get the error that "cannot instantiate type SeekBar.OnSeekBarChangeListener()".
I thought it was due to the fact that OnSeekBarChangeListener was an interface, so I tried to create a random class that implemented OnSeekBarChangeListener and instantiate that class, but that did not work either.
So, now is my question; how to proceed from here?
Regards and thanks in advance
Erik
EDIT
Added my other classes and XML files. Plus, I no longer get the error on the line with instantiating the OnSeekBarChangeListener thing.
Regards
Erik
You cannot in instantiate OnSeekBarChangeListener because it's an interface. If you create a class which implements this interface, and use an instance of that class as the parameter of setOnSeekBarChangeListener, that should definitely work. The easiest way is letting your Activity implement OnSeekBarChangeListener, and pass this to the setter method.
public class SettingsPage extends Activity implements SeekBar.OnSeekBarChangeListener {
// ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_page);
SeekBar BackLightControl = (SeekBar) findViewById(R.id.action_settings);
BackLightControl.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// ...
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// ...
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// ...
}
// ...
}
Side note: you should use standard Java naming conventions, variable names start with lowercase characters.

Android application restarts when opened by clicking the application icon

I am new to the Android development world and I've built a simple "Hello World" app. First, activity requests a text. When the "Go" button is clicked, the app launches the second activity displaying the input text.
If I click the HOME button and then click the application icon, the app launches the first activity again but if I press-hold the home button and click the icon from the "Recent apps" bar, it resumes the app where I left.
How do I avoid this?
I need my app to resume even if the launcher icon is clicked.
MainActivity.java,
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view){
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txtName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
DisplayActivity.java,
package com.example.myfirstandroidapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public void onDestroy(){
super.onDestroy();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/txtName"
android:layout_alignParentRight="true"
android:onClick="sendMessage"
android:text="Go!" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtName"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Please input your name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_display_message.xml,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
AndroidManifest.xml,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstandroidapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstandroidapp.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="com.example.myfirstandroidapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstandroidapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstandroidapp.MainActivity" />
</activity>
</application>
</manifest>
Problem:
I'm not qualified to say this a bug, but there is a behaviour with release builds when starting the application from the launcher. It seems that instead of resuming the previous Activity, it adds a new Activity on top. There is a related bug report on this topic here.
Solution:
I'm working around this this by closing the Launcher Activity if it's not the root of the task, as a result the previous Activity in that task will be resumed.
if (!isTaskRoot()) {
finish();
return;
}
Issue for me was whenever app in installed by an apk with the click on 'Open' option it used to relaunch every time when we minimize it.
resolved it by
SplashActivity.java:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!isTaskRoot
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intent.action != null
&& intent.action.equals(Intent.ACTION_MAIN)) {
finish()
return
}
}

Changing an Activity in Android

I'm just learning android here, and Java to be honest. I'm just trying to switch to a different activity via a button click, however, it keeps crashing. It crashes when I click the button and go to make the switch. Can someone please help me figure where I'm going wrong?
First Activity:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.*;
public class TicTacToe extends Activity {//Start TicTactToe Class
//CONSTANTS
//Variables
Button mainButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
mainButton = (Button) findViewById(R.id.bPlayNow);
mainButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), playerMenu.class);
startActivity(i);
}
});
}
}//End TicTacToe Class
Activity I'm jumping to:
package com.example.killacatoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class playerMenu extends Activity {
Button bOnePlayer, bTwoPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bTwoPlayer = (Button) findViewById(R.id.bTwoPlayer);
bTwoPlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the XML for the first activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".TicTacToe" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="50dp"
android:text="Welcome to \nTic-Tac-Toe"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bPlayNow"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textSize="34dp"
android:text="Play now!" />
</RelativeLayout>
XML for the second activity:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_x="78dp"
android:layout_y="30dp"
android:text="GAME MODE"
android:textColor="#FFFFFF"
android:textSize="50dp" />
<Button
android:id="#+id/bOnePlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="160dp"
android:text="Single Player"
android:textSize="25dp" />
<Button
android:id="#+id/bTwoPlayer"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_x="25dp"
android:layout_y="220dp"
android:text="Two Player"
android:textSize="25dp" />
</AbsoluteLayout>
For starting a new activity, it is necessary to add it in the manifest file.
<application >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
then in onCreate(), for starting a new activity when the button is pressed, you can do:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
bOnePlayer = (Button) findViewById(R.id.bOnePlayer);
bOnePlayer.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TicTacToe.this, playerMenu.class);
startActivity(intent);
}
});
}
Make sure that you define playerMenu in the AndroidManifest file.
<application ... >
...
<activity
android:name="com.example.killacatoe.playerMenu" >
</activity>
</application>
Check this page for more information
http://developer.android.com/training/basics/firstapp/starting-activity.html
Are you sure that you have register your PlayerMenu activity in AndroidManifest.xml ?
...
please paste the error log.
use this code in your first Activity to jump to another
onClick(View v){
Intent ps = new Intent (TicTacToe.this,
PlayerMenu.class);
startActivity(ps);
}
Manifest File :
<Activity
android:name="com.example.killacatoe.TicTacToe">
</Activity>

new to android development- cant start a new activity [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
First- sorry if my question a bit dumb, I'm totally new to Android development ...
I'm trying to start a new activity from my "LAUNCHER" activity and run it on the emulator, but every time I click on the "Next" button (which should start my second activity), I get an error msg saying: "unfortunately, Omis hang man free has stopped", and then closes the app...
also, when I re-launch the same app in the emulator without re-installing it from Eclipse, it doesn't even showing me the first screen...
I don't even know where my problem lies, so I attached 5 codes:
1'st activity layout (named "activity_open")
<?xml version="1.0" encoding="utf-8" ?>
<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=".OpenActivity"
android:background="#DCDCDC" >
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<TextView
android:id="#+id/tvEnterName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/stEnterName"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="40" >
<EditText
android:id="#+id/etNameInput"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="#string/stHintNameEnter"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_weight="30" >
<Button
android:id="#+id/bNext"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/stNext"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
1'st activity class (named "OpenActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OpenActivity extends Activity {
TextView tvEnterYourName;
EditText etName;
Button bContinue;
String myName, tmp;
final String errorStr = "\nYou must enter a name!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
initialize();
bContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myName = etName.getText().toString();
if(etName.getText().toString().equals("") == true)
{
tvEnterYourName.setText(tmp + errorStr);
tvEnterYourName.setTextColor(Color.RED);
}
else
{
try {
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
private void initialize() {
// TODO Auto-generated method stub
tvEnterYourName = (TextView)findViewById(R.id.tvEnterName);
etName = (EditText)findViewById(R.id.etNameInput);
bContinue = (Button)findViewById(R.id.bNext);
myName = "~";
tmp = tvEnterYourName.getText().toString();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
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.activity_open, menu);
return true;
}
}
2'nd activity class (named "GameActivity")
package com.omi.hangmanfree;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.widget.TextView;
public class GameActivity extends Activity {
TextView tt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
GridView gvGrid = (GridView)findViewById(R.id.gvHagdara);
tt.setText("_");
gvGrid.addView(tt);
}
#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_game, menu);
return true;
}
}
2'nd activity layout (named "layout_game")
<?xml version="1.0" encoding="utf-8" ?>
<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=".GameActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<GridView
android:id="#+id/gvHagdara"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/textView1"
android:layout_centerInParent="false"
android:numColumns="auto_fit" >
</GridView>
</RelativeLayout>
5. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omi.hangmanfree"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".OpenActivity"
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=".GameActivity"
android:label="#string/title_activity_game" >
<intent-filter>
<action android:name="com.omi.hangmanfree.GAMEACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I'm not looking for code improvements (I am only learning the language), so please do not send me suggestions to improve the code (I know there are a lot!) --- I just want this code to work.
The problem may be in the Intent. Try this:
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
Please Remove finish(); from onPause() Method from OpenActivity and remove gvGrid.addView(tt); from GameActivity because gridview is not container layout and write below code
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);
instead of
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
it will solve your problem.
please replace in openActivity
Intent gameIntent = new Intent(getApplicationContext(), GameActivity.class);
startActivity(gameIntent);
Try changing here:
Intent gameIntent = new Intent("com.omi.hangmanfree.GAMEACTIVITY");
startActivity(gameIntent);
to
Intent gameIntent = new Intent(OpenActivity.this, GameActivity.class);
startActivity(gameIntent);

Categories

Resources