Android Menu Inflater error - java

Im trying to get my menu inflater to link to my layout file, but when app launches and I hit menu, then select menu option it crashes program. It says it cannot find activity to launch:
error in eclipse LogCat:
01-01 10:24:38.799: E/AndroidRuntime(30359): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.menu/com.menu.AboutUs}: java.lang.ClassNotFoundException: com.menu.AboutUs in loader dalvik.system.PathClassLoader[/data/app/com.menu-1.apk]
Main Code:
package menu;
import com.menu.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
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);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.new_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent("menu.ABOUT");
startActivity(i);
break;
case R.id.settings:
break;
}
return false;
}
Manifest code:
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="menu.testActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AboutUs"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog">
<intent-filter >
<action android:name="menu.ABOUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
about.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:text="Why dosent this work??????????????????????? "
android:title="About Us"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I would gratefully appreciate help... thank you

If you want to launch say Activity B from Activity A, you should make sure that you have declared Activity B in your manifest file.Then in your onOptionsItemSelected(), you can say,
Intent i = new Intent(this,B.class);
startActivity(i);
If you want to start activity using Action parameter for intent you can use the following in the Activity tag in the mafifest.
This is considering activity B is in the application package directly
<activity android:name=".B">
<intent-filter>
<action android:name="menu.ABOUT" />
</intent-filter>
</activity>

You got Class not found exception for com.menu.AboutUs - pretty straightforward

Related

Errors while adding splash screen in android

I am new to android development, and I'm trying to add a splash screen in my app.
I found this tutorial:
I have added the files as mentioned in the tutorial but it doesn't seems to work, there is an error in my Splashscreen.java file
It gives me the error cannot resolve symbol androidmkab
Thanks for helping in advance.
Splashscreen.java
package com.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.ImageView;
import com.androidmkab.randomsplash.MainActivity;
import java.util.Random;
public class Splashscreen extends Activity {
Thread splashTread;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
imageView = (ImageView)findViewById(R.id.imageView2);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
int[] ids = new int[]{R.drawable.s_img,R.drawable.s_image_black, R.drawable.s_image_black2};
Random randomGenerator = new Random();
int r= randomGenerator.nextInt(ids.length);
this.imageView.setImageDrawable(getResources().getDrawable(ids[r]));
splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3500) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(Splashscreen.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
Splashscreen.this.finish();
} catch (InterruptedException e) {
// do nothing
} finally {
Splashscreen.this.finish();
}
}
};
splashTread.start();
}
}
activity_splash.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:background="#ffffff"
android:layout_gravity="center"
android:id="#+id/lin_lay"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/splash"
android:background="#drawable/logo" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplication">
<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=".Splashscreen"
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=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
import com.androidmkab.randomsplash.MainActivity; remove this line from the top. Its not your package... instead make it import com.myapplication.MainActivity
the problem seems to be that you are trying to call a package which does not exist in your project. androidmkab.randomsplash.MainActivity should be replaced with your project package. such as com.myApplication.MainActivity
Delete the line com.androidmkab.randomsplash.MainActivity; and write com.myapplication.MainActivity;

can't add search button on action bar

So i'm new to android programming
im going to add two action bar , but when i add the second action bar , it fills the old one , not making a new one .
i already use the holo light theme .
this is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orionpreneur"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow">
<!-- Splash Screen Activity -->
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/about_us"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
<activity
android:name=".About"
android:label="#string/title_activity_about"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
</application>
</manifest>
this is my main_activity_menu.xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/about"
android:title="#string/about"
android:showAsAction="always"/>
</menu>
this is my MainActivity.java code
package com.example.orionpreneur;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class LoginActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
#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_activity_menu, menu);
return super.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:
Toast.makeText(this, "you've pressed the search button !", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Intent in = new Intent(this, About.class );
startActivity(in);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
i wanted the search button to be added in separated bar like this
https://developer.android.com/images/training/basics/actionbar-actions.png
i'm desperate , sorry if my question is already asked .
thanks :)))
*remove this *
android:uiOptions="splitActionBarWhenNarrow"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen

How to start an IntentService from a WakefulBroadcastReceiver

I have an application, which you should be able to recreate entirely and very easily with the code I'll post in this question. Here's the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcasttest.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="com.example.broadcasttest.TestReceiver"
android:label="#string/app_name"
android:enabled="true" >
</receiver>
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
</application>
</manifest>
As you can see, the contains an activity, a (wakeful) broadcast receiver and an intentservice, all in the same package. The activity gets started at launch, here's the code:
package com.example.broadcasttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBroadcast(new Intent(this, TestReceiver.class));
}
#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);
}
}
This succesfully triggers the onReceive function of TestReceiver.
package com.example.broadcasttest;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class TestReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Intent service = new Intent("com.example.broadcasttest.MonitorService");
Intent service = new Intent(context, MonitorService.class);
startWakefulService(context, service);
}
}
This is where things go wrong though, I placed a breakpoint in the onReceive function and it definitely gets called. However, the MonitorService class never gets reached. I placed a breakpoint in the onHandleEvent function, but it seems like it never gets that far. Here's the code for this class:
package com.example.broadcasttest;
import android.app.IntentService;
import android.content.Intent;
public class MonitorService extends IntentService {
public MonitorService(String name) {
super(name);
}
public MonitorService()
{
super("MonitorService");
}
#Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
TestReceiver.completeWakefulIntent(intent);
}
}
}
As you can tell from the commented line in the TestReceiver class, I've tried using an implicit intent instead of an explicit one. I've also read this question and tried everything mentioned there. Am I missing something here? I'm running this on an emulator (Nexus7 API L).
Is there anything I'm missing here?
There is no tag as <intentservice> in Application Manifest. IntentService is a subclass of Service, so you need to declare it as service in manifest.
Change
<intentservice
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>
to
<service
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</service>

My second activity layout doesn't start when I click it

Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.colegiul.orar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".StartingPoint" android:label="#string/app_name" >
<intent-filter>
<action android:name="com.colegiul.orar.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ourClass"
android:label="#string/app_name" >
</activity>
<activity
android:name=".Menu" android:label="#string/app_name" >
<intent-filter>
<action android:name="com.colegiul.orar.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Menu.java:
package com.colegiul.orar;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] ={"StartingPoint", "Clasa a 9-a A", "Clasa a 9-a B"
, "Clasa a 9-a C", "Clasa a 9-a D", "Clasa a 9-a E", "Clasa a 9-a F"};
#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) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese =classes[position];
try{
Class ourClass = Class.forName("com.colegiu.orar." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
StartingPoint.java
package com.colegiul.orar;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class StartingPoint extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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.starting_point, menu);
return true;
}
}
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=".StartingPoint" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/orar" />
</RelativeLayout>
My app is strarting with a splash screen which works perfectly, and opens a new menu, which contains a list with 7 examples and the first one "StartingPoint" should open a new activity, but it doesn't, and my head almost blew up trying to figure out why was that.. can anyone tell my please, why is doesn't work properly? Thanks.
O, PS:
Please be very patient and explain me detailed bacause I am a newbie.
Your onListItemClick has something like:
Class ourClass = Class.forName("com.colegiu.orar." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
and your manifest has a packaged name defined as: package="com.colegiul.orar".
Obviously won't work!
You should replace the above line by the following one
Class ourClass = Class.forName("com.colegiul.orar." + cheese);
EDIT:
After this, you have another problem instanciating your Intent because your cheese values are not declared as activities in manifest.
String classes[] ={"StartingPoint", "Clasa a 9-a A", "Clasa a 9-a B"
, "Clasa a 9-a C", "Clasa a 9-a D", "Clasa a 9-a E", "Clasa a 9-a F"};
Your classes array only contains one Activity name declared in app's manifest. The others are not. (Actually you can't declare activities with spaces - they are not valid!!) Thus you should be faced with a message in the stacktrace like "Are you declared this Activity in the Manifest" or an exception could be thrown (ClassNoFoundException).
Two points:
1) put .class after ourClass, while calling ourClass activity. Like this
Intent ourIntent = new Intent(Menu.this, ourClass.class);
startActivity(ourIntent);
2) Your second activity is not registered in manifest.xml.Register it like this
<activity
android:name=".ourClass"
android:label="#string/app_name" >
</activity>
I think the problem is that you not declare your second activity in your Manifest.
For each Activity, you need to declare it in your Manifest.xml as below:
<activity
android:name="com.colegiul.national.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.colegiul.national.SecondActivity"
android:label="#string/app_name" >
// You declared the Intent-filter as an Activity. This is wrong. An Intent-filter
// is to inform the system of the capability of the component, not the name of your
// Activity or the package. It is to the say what is your Activity will do.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.colegiul.national.ThirdActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And also, as #Vikram said, you need to call your Intent like this:
Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(myIntent);
You need to set the Context at first parameter and the Class at the second. See this documentation for more details: Build Intent Documentation
EDIT:
In your case, try to create your Intent as below:
String packClass = "com.colegiul.orar." + cheese;
Intent ourIntent = new Intent(Menu.this, Class.forName(packClass));
startActivity(ourIntent);

Intent using thread is not working

I know I already asked this, but I didn't get a sufficient answer. Im trying to start an activity, but the emulator stays on the first activity. Ive tried all ways to do it but it never works. The youtube videos show that it should work but it never does. Is there something missing or is there anything wrong with the following code?
//First Activity:
package com.mtprogramming.magicsquaresgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
public class Opening extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
Intent open = new Intent("com.mtprogramming.magicsquaresgame.MENU");
startActivity(open);
}
}
};
timer.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.opening, menu);
return true;
}
}
//Second Activity:
package com.mtprogramming.magicsquaresgame;
import android.app.Activity;
import android.os.Bundle;
//Created by suprav on 7/11/13.
public class Menu extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
}
//Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mtprogramming.magicsquaresgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
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.mtprogramming.magicsquaresgame.Opening"
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.mtprogramming.magicsquaresgame.Menu"
android:label="#string/title_activity_menu"
android:parentActivityName="Opening" >
<intent-filter>
<action android:name="com.mtprogramming.magicsquaresgame.MENU"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="Opening" />
</activity>
</application>
</manifest>
Try this:
Intent open = new Intent(Opening.this, Menu.class);
startActivity(open);
I think i found the issue, seems like metadata property is not being properly used, hences activity its not being started, this is the proper way to use the property:
<activity android:name=".TestActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".TestParentActivity">
</meta-data>
</activity>
So, seems like you are missing a dot in "android:value="Opening"
Regards!

Categories