here I am again.
I have a problem with my menu in combination with my activities.
When I run my emulator, and click the uppest item in the list, the emulator opens all list activities.
Hopefully someone can help me.. Thank you all in advance!
My Menu.Java:
package com.jacob.eindproject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity implements OnItemClickListener {
String classes[] = { "BMI- Calculator", "Ondergewicht", "Gezond Gewicht", "Overgewicht"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//Positie 0 is het eerste item (dus de BMI-Calculator.)
super.onListItemClick(l, v, position, id);
switch(position)
{
case 0:
Intent openStartingPoint = new Intent(getApplicationContext(),MainActivity.class);
startActivity(openStartingPoint);
case 1:
Intent openOndergewicht = new Intent(getApplicationContext(),Ondergewicht.class);
startActivity(openOndergewicht);
case 2:
Intent openGezondgewicht = new Intent(getApplicationContext(),Gezond_gewicht.class);
startActivity(openGezondgewicht);
case 3:
Intent openOvergewicht = new Intent(getApplicationContext(),Overgewicht.class);
startActivity(openOvergewicht);
break;
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jacob.eindproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.jacob.eindproject.Menu"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.jacob.eindproject.Inleiding"
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.jacob.eindproject.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.jacob.eindproject.Ondergewicht"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.jacob.eindproject.activity.ONDERGEWICHT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:name="com.jacob.eindproject.Gezond_gewicht"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.jacob.eindproject.activity.GEZOND_GEWICHT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:name="com.jacob.eindproject.Overgewicht"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.jacob.eindproject.activity.OVERGEWICHT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
</application>
First, there opens the Inleiding, it's my logo, and after 5 seconds it disappears.
After that, I want the menu with 4 items, 1 for the MainActivity.java, wich is a BMI-calculator. The last 3 items are informational items, about weight and stuff.
Somebody who knows the answer?
Thank you all for your efforts.
The reason all your activities open is because you don't have break statements at the end of each case clause in your switch. You need to add a break at the end of each case block.
Also, you should remove the <intent-filter> tags from all of your activities except the first one (Inleiding) that contains CATEGORY=DEFAULT and ACTION=MAIN. You don't need those and they will just confuse you.
Related
When I add
<action android:name="android.intent.action.MAIN" />
in the manifest file, the application starts from the 2nd activity.
If I don't put the action,
<action android:name="android.intent.action.MAIN" />
then the application does not start.
If I remove
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The Gradle gives an error.
"Error running Main2Activity: The activity must be exported or contain an intent-filter"
here is my code for manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell_7560.experiment3">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".Main2Activity"
android:label="#string/title_activity_main2">
<!--if I remove this, I get an error mentioned above-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity 1 file
package com.example.dell_7560.experiment3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=(LinearLayout)findViewById(R.id.ll1);
ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
}
}
activity 2 file
package com.example.dell_7560.experiment3;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
poupulateListView();
}
public void poupulateListView() {
//get the values from res
Resources res=getResources();
String data_items[]=res.getStringArray(R.array.details);
//build a adapter
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,
android.R.layout.simple_list_item_1,//using a default layout
data_items)//elements fed to the adapter
{
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view=super.getView(position,convertView,parent);
TextView tv=(TextView)view.findViewById(android.R.id.text1);
tv.setTextColor(getResources().getColor(R.color.occurYellow));
return view;
}
};
//why do we android before R? cause normally we use R.id.something
//configure the adapter
ListView lv=(ListView) findViewById(R.id.list_view_1);
lv.setAdapter(adapter);
}
}
manifest file only add between application tag below line.
<activity android:name=".Main2Activity"
android:label="#string/title_activity_main2"/>
In manifest file only first activity which call on app start that activity add only intent filter other then activity only called above code.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
The intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In the manifests tells the launcher which is the main activity to launch. Those 2 things must be present so it is recognised as an activity to be launch.
If you have 2 activities with this intent filter, and from the first one you remove one of the parameters, its not recognised anymore and the next one matching will be used. In this case, your second activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This is to indicate that this activity is the activity that android must be start when open the app. You should have one activity with this.
I have tried this on a few android phones with no luck (i did start the app once).
This was mostly generated from android studio 1.3.2 and from some questions like this and that and the other.
Perhaps someone can point out what i am doing wrong.
Thanks
edit: moved receiver inside app, but no joy.
edit2: added missing permissions for receiver. now seems to work on a nexus 4. but not on the kindle fire or the at&t tablets. although it seems to come right away if i press the circle icon on the azpen.
edit3: seems to work sometimes on the fires now.
package acme.startup;
import android.content.*;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
package acme.startup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
</application>
</manifest>
Every Component must be declared inside application tag into manifest. Here you declared receiver outside of application tag. Read Structure of the Manifest File.
Correct manifest wil be
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have to add permission to manifest file. Add this before your <application> tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I am facing a issue where my game becomes invisible in the menu after installation. Also when .apk installed, OPEN button becomes disabled and i'm unable to launch my game.
I made this game in Unity3D. I want to do deep-linking in my game so created my own Activity named as MainActivity extends UnityPlayerActivity and also did some changes in the AndroidManifest.xml file.
MainActivity.java
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("MainActivity", "onCreate called!");
}
#Override
public void startActivity(Intent intent)
{
boolean isStartActivity = true;
Uri uri = intent.getData();
if (uri != null && uri.getScheme().equals("abc"))
{
isStartActivity = false;
}
if (isStartActivity)
{
super.startActivity(intent);
}
else
{
Log.d("MainActivity", "MainActivity.startActivity() : uri = " + uri.toString());
UnityPlayer.UnitySendMessage("Main Menu", "JuliusReward", uri.toString());
}
}
}
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.xyz">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<!-- android:debuggable should be removed in release build -->
<application
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:debuggable="true">
<activity android:name="com.abc.xyz.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "myapp://com.example.myapp” -->
<data android:scheme="abc" android:host="com.example.abc"/>
</intent-filter>
</activity>
<activity android:name="org.onepf.openiab.UnityProxyActivity"
android:launchMode="singleTask"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
<receiver android:name="com.amazon.device.iap.ResponseReceiver">
<intent-filter>
<action
android:name="com.amazon.inapp.purchasing.NOTIFY"
android:permission="com.amazon.inapp.purchasing.Permission.NOTIFY" />
</intent-filter>
</receiver>
</application>
<!--all-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--Google Play-->
<uses-permission android:name="com.android.vending.BILLING" />
<!--For Tablets-->
<uses-feature android:name="android.hardware.telephony" android:required="false" />
</manifest>
Please check this post Intent filter for launcher and send activity
You should have an
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
For my Android App, I've declared multiple Activities in the AndroidManifest.xml file.
The AndroidManifest.xml file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.btdt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/quizicon"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:debuggable="true">
<activity android:name="QuizActivity"
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="QuizSplashActivity"> </activity>
<activity android:name="QuizGameActivity"> </activity>
<activity android:name="QuizHelpActivity"> </activity>
<activity android:name="QuizMenuActivity"> </activity>
<activity android:name="QuizScoresActivity"> </activity>
<activity android:name="QuizSettingsActivity"> </activity>
</application>
</manifest>
For some reason, my App never shows up on the Emulator.
I've read the answers of other people asking the same question, but my App still does not show.
I do not get any Compilation or Running Errors.
If it's any help, my QuizActivity.java file is
package com.example.btdt;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.TextView;
public class QuizActivity extends Activity {
TextView countDisplay;
public static final String GAME_PREFERENCES = "GamePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_splash);
SharedPreferences settings =
getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("UserName", "Ryan D'souza");
prefEditor.putInt("UserAge", 16);
prefEditor.commit();
countDisplay = new TextView(this);
this.setContentView(countDisplay);
if(settings.contains("UserName") == true)
{
String user = settings.getString("UserName", "Default");
countDisplay.setText(user);
}
}
#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_splash, menu);
return true;
}
}
Thank you for your help
Try To declare your other activities like that:
<activity
android:name=".YourClass"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.your.package.name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If the emulator does not show it, restart Eclipse and open it. Before to run the app, open the emulator and wait still it load.
So I am trying to run this app I am using to learn about threads and intents however the app will not run, But I don't have any warning/error underlines in the code, could anyone help me please.
Manifest code:
<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.learn.tam.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.learn.tam.StartingPoint"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
and Activity code
package com.example.learn.tam;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent openStartingPoint = new Intent("com.example.learn.tam.StartingPoint");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
any help would be appreciated please
Change
<intent-filter>
<action android:name="android.intent.action.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
to
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Since your app is probably not shown on the home screen (the MAIN category is needed).
And also change
Intent openStartingPoint = new Intent("com.example.learn.tam.StartingPoint");
startActivity(openStartingPoint);
to
Intent openStartingPoint = new Intent(Splash.this, StartingPoint.class );
startActivity(openStartingPoint);
Since your StartingPoint does not define an Intent-Action with "com.example.learn.tam.StartingPoint"
See if that fixes anything.
Remove this:
<action android:name="android.intent.action.SPLASH" />
And replace it with this:
<action android:name="android.intent.action.MAIN" />