java.lang.StackOverFlowError in android studio - java

i am working as a beginner on new android application, i have done everything according to the tutorials, but im still getting this error. here is the code for MyAcitivty.java:
this is MYActivity.java
package com.example.ambuj.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.ambuj.myfirstapp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void openSearch(){
Toast.makeText(this, "Search Button Pressed", Toast.LENGTH_LONG).show();
}
public void openSettings(){
Toast.makeText(this, "Settings Button Pressed",Toast.LENGTH_LONG).show();
}
}
this is my logcat:
01-18 14:22:33.180 26194-26194/com.example.ambuj.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ambuj.myfirstapp, PID: 26194
java.lang.StackOverflowError
at android.util.SparseArray.get(SparseArray.java:111)
at android.util.SparseArray.get(SparseArray.java:102)
at android.content.res.StringBlock.get(StringBlock.java:70)
at android.content.res.AssetManager.getPooledString(AssetManager.java:275)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:730)
at android.content.res.TypedArray.getText(TypedArray.java:97)
at android.support.v7.internal.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:374)
at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:168)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:32)
at com.example.ambuj.myfirstapp.MyActivity.onCreateOptionsMenu(MyActivity.java:33)

Yout problem is here:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return onCreateOptionsMenu(menu); //<--- conflicting line
}
Change
return onCreateOptionsMenu(menu);
to
return true;
Hope it helps

Related

Android Studio Button Creation

I am trying to create a page where I have four buttons, each leading to a separate class in the file. So if button 1 is pressed it takes you to that class. I think I have done the basics but don't know about creating the menu.
package com.example.a.myapplication;
import android.content.Intent;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#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.menu_main, menu);
return true;
super.onCreateOptionsMenu (menu);
MenuItem button = menu.add(0, 0, Menu.NONE, "MainActivity");
MenuItem button1 = menu.add(0, 1, Menu.NONE, "second");
MenuItem button2 = menu.add(0, 2, Menu.NONE, "third");
MenuItem button3 = menu.add(0, 3, Menu.NONE, "fourth");
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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
{
switch (item.getItemId())
{
case 0:
Intent i1 = new Intent(this, MainActivity.class);
startActivity (i1);
return true;
case 1:
Intent i2 = new Intent(this, second.class);
startActivity (i2);
return true;
case 2:
Intent i3=new Intent(this, third.class);
startActivity(i3);
return true;
case 3:
Intent i4=new Intent(this, forth.class);
startActivity(i4);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
}
Modify /res/menu/main.xml to define menu items
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_up"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/arrow_up_float"
android:title="Up"/>
<item
android:id="#+id/action_down"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:icon="#android:drawable/arrow_down_float"
android:title="Down"/>
<item
android:id="#+id/action_other"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_launcher"
android:title="Other"/>
In the main activity's onOptionsItemSelected, handle the menu items.
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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity (intent);
break;
case R.id.action_up:
Intent intent = new Intent(this, SecondActivity.class);
startActivity (intent);
break;
case R.id.action_down:
Intent intent = new Intent(this, ThirdActivity.class);
startActivity (intent);
break;
case R.id.action_other:
Intent intent = new Intent(this, FourthActivity.class);
startActivity (intent);
break;
default:
super.onOptionsItemSelected(item);
break;
}
//Return false to allow normal menu processing to proceed,
//true to consume it here.
return false;
}
}

Android Studio, Call to different class in different file

I have written the piece of code below to reference to the class called Forecast Fragment, and the app runs. but on line 33, i am getting a error for "cannot resolve symbol 'android' "
package com.example.sunshinepre_beta;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
int commit = getSupportFragmentManager().beginTransaction()
.add(R.id.container, new com.example.android.sunshinepre_beta.app.ForecastFragment())
.commit();
}
}
#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);
}
}
Replace new com.example.android.sunshinepre_beta.app.ForecastFragment() with new ForecastFragment()
Also move ForecastFragment.java to the same folder where MainActivity is.

java - need help for keycode_home [duplicate]

This question already has answers here:
Keycode_home doesn't get called ANDROID
(4 answers)
Closed 8 years ago.
i need help in my app which shows a toast when home button is pressed, back button is pressed and it check if the phone have got the nav bar
MainActivity.java
package com.example.myapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(getApplicationContext(), "goodbye! (home button pressed)", Toast.LENGTH_LONG).show(); //doesn't work here
finish();
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Toast.makeText(getApplicationContext(), "goodbye! (back button pressed)", Toast.LENGTH_LONG).show();
finish();
return true;
}
return false;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
// no navigation bar
Toast.makeText(getApplicationContext(), "no nav bar", Toast.LENGTH_LONG).show();
} else {
// navigation bar
Toast.makeText(getApplicationContext(), "nav bar", Toast.LENGTH_LONG).show();
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I have got a nexus 5 so it has the nav bar, and I think this is the problem
how can I fix the prolem?
nexus 5, android 4.4.3
thanks
The framework never sends KEYCODE_HOME to apps.
Right now your best indicator of when the user leaves your app (as opposed to a single activity), is listening for TRIM_MEMORY_UI_HIDDEN in onTrimMemory(int).
public class MainActivity extends Activity {
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_UI_HIDDEN) {
Context ctx = getApplicationContext();
Toast.makeText(ctx, "gone", Toast.LENGTH_SHORT).show();
}
}
}
Also you should not finish() just because the user leaves, then when the user gets back the activity will always have to be recreated from scratch, without state.

Android app crashes, due to count down timer?

I have made an app recently and I added a count down timer one day and made a few changes here and there and now when I run the app on my (actual) Galaxy Note 3 it crashes as soon as I hit the Play button below are my LevelOne.java file and my MainActivity.Java file. By the way the logcat shows nothing as if it doesn't realize it crashed.
LevelOne.java
package com.parker.orangedot;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class LevelOne extends ActionBarActivity {
Button myButton;
LevelOne context;
CountDownTimer timer = new CountDownTimer(1500, 1500)
{
#Override
public void onFinish() {
Intent intent = new Intent(context, Scores.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_level_one);
//ass soon as this activity is created, I start my timer.
timer.start();
myButton = (Button) findViewById(R.id.button2);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
timer.cancel();
}});
}
#Override
public void onBackPressed() {
}
public void next(View view) {
// Do something in response to button
Intent intent = new Intent(this, LevelTwo.class);
startActivity(intent);
}
public void openScores(View view) {
// Do something in response to button
Intent intent = new Intent(this, Scores.class);
startActivity(intent);
}
#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_one, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_level_one,
container, false);
return rootView;
}
}
}
MainActivity.java
package com.parker.orangedot;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void openScores(View view) {
// Do something in response to button
Intent intent = new Intent(this, Scores.class);
startActivity(intent);
}
public void play(View view) {
// Do something in response to button
Intent intent = new Intent(this, LevelOne.class);
startActivity(intent);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
UPDATE:
When using the emulater for the Galaxy Nexus, pressing "play" does absolutely nothing.

My PopUpMenu doesn't do anything

I'm working on the Android Developer Tutorial and when I tried to open a activity it doesn't do anything.
Here is my Main.java:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(), "Probandooo", Toast.LENGTH_LONG).show();
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.main, popup.getMenu());
popup.show();
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.responder:
Intent i = new Intent (Main.this, Responder.class);
startActivity(i);
return true;
case R.id.reenviar:
Toast.makeText(getApplicationContext(), "Reenviando...", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
#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;
}
}
I already declare it on my manifest, I don't know what is

Categories