ArrayIndexOutOfBoundsException whie starting an activity in Android - android

I have just started studying Android and I am trying out a simple application to just display a button which when clicked, to show a popup.
My modules below
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Click me!</string>
<string name="button1">Click me!</string>
<string name="popup_text">This is a text</string>
<string name="popup_title">Hi...</string>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#045FB4</color>
</resources>
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/popup_text"
/>
</ScrollView>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#color/background"
android:padding="30dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="#string/button1" android:id="#+id/clickme_button"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlecode.cowbullgame" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".ButtonTest" 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=".Popup" android:label="#string/popup_title"
android:theme="#android:style/Theme.Dialog">
</activity>
</application>
</manifest>
ButtonTest.java
package com.googlecode.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ButtonTest extends Activity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View clickmeButton = findViewById(R.id.clickme_button);
clickmeButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.clickme_button:
startActivity(new Intent(this, Popup.class));
break;
}
}
}
popup.java
package com.googlecode.test;
import android.app.Activity;
import android.os.Bundle;
public class Popup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
}
}
While clicking the button, I get an ArrayIndexOutOfBoundsException. I couldn't quite figure out where I am going wrong. Please point it
Thanks.

When getting the button from the xml file, cast the view to Button. Use the following,
Button clickmeButton = (Button) findViewById(R.id.clickme_button);
instead of
View clickmeButton = findViewById(R.id.clickme_button);
And by popup what do you mean. Do you want to show a Dialog? Because your code will just start another activity. to get a popup, use Dialogs. Read about AlertDialog here: http://developer.android.com/guide/topics/ui/dialogs.html .

Button clickmeButton = (Button) findViewById(R.id.clickme_button);
no need of theme for PopUP activity.

Related

Cant start new activity. It keeps crashing

When pressing the button, new activity should open but app instantly crashes. I have searched for answer and making a few adjustments to my code and only thing i dont know how to do is Manifest, can u help me with that?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Starting.Programm"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity android:name="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="InformationActivity"
android:label="#string/app_name"
>
</activity>
</application>
MainActivity:
package Starting.Programm;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends InformationActivity
{
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startJourney(View view) {
Intent intent = new Intent(this, InformationActivity.class);
startActivity(intent);
}
}
main.xml
<?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"
tools:context=".MainActivity"
>
<Button android:id="#+id/button_StartApp"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="#string/button_StartApp"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:layout_marginLeft="115dp"
android:onClick="startJourney"
/>
</LinearLayout>
And next activity which should be opening
package Starting.Programm;
import android.app.Activity;
import android.os.Bundle;
class InformationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
}
}
<activity> android:name=".InformationActivity" </activity>
Remove android:label="#string/app_name" too. I don't think it is needed.
Add dot to end of pacakager name
package="Starting.Programm."

How to make two activities?

I'm new in Android studio and I'm trying to make two activities in my application but it don't works.
I don't know what I'm doing wrong, I think it's about the first options I put in my intent.
I'm new and I would like a simple solution or if you can explain me what I have to do it will be fine :)
This is my first activity (MainActivity):
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mPasserelle = null;
public final static String AGE = "com.myapplis.multiactivite.AGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPasserelle = (Button) findViewById(R.id.passerelle);
mPasserelle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent secondeActivite = new Intent(MainActivity.this,IntentExample.class);
secondeActivite.putExtra(AGE,24);
startActivity(secondeActivite);
}
});
}
}
The second activity(IntentExample) :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentExample extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.seconde_activite);
Intent i = getIntent();
int age = i.getIntExtra(MainActivity.AGE,0);
TextView resultat= (TextView)findViewById(R.id.resultat);
resultat.setText("Le résultat est : "+age);
}
}
This is my first layout (activity_main), just a simple button to push to get the second activity:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dunomade.multiactivite.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/passerelle"/>
</LinearLayout>
And now this is my second layout(seconde_activite), just a simple text to show me I'm on the second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultat"
android:text="" />
</LinearLayout>
And to finish, this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplis.multiactivite">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntentExample"
android:label="seconde acivite">
</activity>
</application>
</manifest>
When I'm running the app, MainActivity is good, but when I try to click on the button the AVD show me "Have you declared this activity in your AndroidManifest ?". I declared it on my manifest but I think the problem is in my MainActivity.
Please help me, I think for you it's really simple but I can't solve it, I tryed many way but I already got the same error.
Thank's for read and for answer =)
I advise that you use the name of package before the name of your activity, like this (some devices need this, like Moto G) :
<activity android:name="your.package.IntentExample "></activity>
And instead use:
public class IntentExample extends Activity
use:
public class IntentExample extends AppCompatActivity

Android - Popup dialog on home/native screens

edit: The problem was trying to display the dialog and finish() the activity the next line, so there was no view for the dialog to show! Would be easier if I could just set home-screen as the context for the dialog.
Newbie here. I know this question has been asked for many times, but there's no full picture on the solution so far. I first started to run some code without GUI, which is successful by simply setting the theme. Now, I want to add dialog to display error from the code, but I cannot get it to show!
<activity android:name="PopupActivity"
android:launchMode="singleInstance" android:excludeFromRecents="true"
android:taskAffinity="" android:theme="#android:style/Theme.Dialog" />
So I created an empty view with wrap_content as width and height, but I'm not sure what view element I should use for the AlertDialog object in the activity. I have tested all my code in another activity with a view, everything works fine, AlertDialog shows without problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</LinearLayout>
Is there a simple solution? Or I have to treat the Theme.Dialog view as a normal view and add textview and button to emulate a dialog?
I use AlertDialog.Builder for the dialog creation:
AlertDialog.Builder alertDialogB = new AlertDialog.Builder(cont);
alertDialogB.setTitle(title);
alertDialogB.setMessage(msg);
...
...
IDE: IntelliJ
SDK: 4.1.2
You can create simple dialog with different solutions but I used this for starts with.
Here I am adding list of files with code to clarify your doubts:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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.demo.LoginActivity"
android:label="#string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.demo.HomeActivity"
android:label="#string/title_activity_home"
android:theme="#android:style/Theme.Dialog">
</activity>
</application>
</manifest>
LoginActivity.java
package com.demo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
public class LoginActivity extends Activity {
Button login_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn=(Button) findViewById(R.id.btnlogin);
login_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),HomeActivity.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_login, menu);
return true;
}
}
activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity"
android:layout_gravity="center">
<Button
android:id="#+id/btnlogin"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
</LinearLayout>
And one activity which comes as a alert dialog for Login Activity
HomeActivity.java
package com.demo;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}
activity_home.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"
tools:context=".HomeActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Not sure I got your question right, but you use an activity for the AlertDialog.Builder(getActivity);
And then just Dialog dialog = builder.create();
dialog.show();

my android code for creating and using the menu does not work as it should

I am a new bee to Android App Development. I am practicing the code in the Book Hello, Android . Right now I am struck with Menu and could not able to move further. I would like to present my situation in clear so that I could be guided by an experienced android app developer . I uploaded a picture showing the status.
when I pressed the menu I could able to find one option called settings.
But nothing happens when the settings button is pressed. I am surprised about the error and wasted lot of time to fix it. but I could not able to fix it. I want some real android app development professional to find the solution to this problem that I face and help me to proceed further.
here is the code that i used.
test/src/org/anil/test/TestActivity.java
package org.anil.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsMenuSelected( MenuItem item){
switch(item.getItemId()){
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
}
return false;
}
}
test/res/layout/main.xml
<?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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
test/src/org/anil/test/Prefs.java
package org.anil.test;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
test/res/menu/menu.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/settings"
android:title="#string/settings_label"
android:alphabeticShortcut="#string/settings_shortcut" />
</menu>
test/res/xml/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="music"
android:title="#string/music_title"
android:summary="#string/music_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="hints"
android:title="#string/hints_title"
android:summary="#string/hints_summary"
android:defaultValue="true"/>
</PreferenceScreen>
test/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestActivity!</string>
<string name="app_name">Test</string>
<string name="settings_label">Settings</string>
<string name="settings_title">Sudoku Settings</string>
<string name="settings_shortcut">s</string>
<string name="music_title">Music</string>
<string name="music_summary">play background music</string>
<string name="hints_title">Hints</string>
<string name="hints_summary">Show hints during play</string>
</resources>
test/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.anil.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
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=".Prefs" android:label="#string/settings_title">
</activity>
</application>
</manifest>
The name of method for callback for menu selection is onOptionsItemSelected() instead of onOptionsMenuSelected(), try this way:
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
//Your code implementation
}
Is your Prefs activity in your manifest?
I might be wrong but I'll give a shot:
In line
startActivity(new Intent(this, Prefs.class));
I think "this" does not represent your activity. Try create a static variable
static Activity thisActivity;
and on OnCreate
thisActivity = this;
then
startActivity(new Intent(thisActivity, Prefs.class));

Got to location on map activity with click from another class

My app has three tabs, one of which is a Map. From the other two tabs I want the user to be able (after navigation through several screens) to go to the map tab and a certain location on the map with a button click from one of the screens which the user ends up on from the other two tabs. Is this possible?
referenced from method intentTest.xyz.com.intentTest$1.onClick
1.intentTest.java:
package intentTest.xyz.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class intentTest extends Activity {
Button b;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.b);
b.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(intentTest.this,second.class );
startActivity(intent);
}
});
}
}
2.main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First screen"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="map"
android:id="#+id/b"
/>
</LinearLayout>
3.second.java:
package intentTest.xyz.com;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class second extends MapActivity{
MapView map;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
map = (MapView) findViewById(R.id.map);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
4.second.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0ujyc9Tw2cYvyPECIKTQIK0pwuL-UPa_sh4BpIw"
/>
5.manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="intentTest.xyz.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".intentTest"
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=".second"
android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

Categories

Resources