Beginner working on an android app and I'm trying to start a searchable activity when the onSearchRequested method is called after a button push in the main activity. Currently, when the button is pressed, nothing happens.
Here's MainActivity.Java:
package com.example.activity2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onSearchRequested();
}
});
}
/** Called when the user clicks the Send button */
// public void sendMessage(View view)
// {
// }
#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);
}
}
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity2"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.FLAG_ACTIVITY_NEW_TASK" />
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.activity2.SearchActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.authorwjf.youtubeapi.MainActivity"
android:label="#string/title_activity_activity2" >
</activity>
<activity android:name=".SearchActivity" >
android:launchMode="singleTop" >
<intent-filter>
<action android:name="com.example.activity2.SEARCHACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="#string/title_activity_other" >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchListView"
android:label="#string/title_activity_search_list_view" >
</activity>
<activity
android:name=".SearchYoutube"
android:label="#string/title_activity_search_youtube" >
</activity>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</application>
</manifest>
SearchActivity.Java
package com.example.activity2;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.api.services.youtube.model.SearchResult;
public class SearchActivity extends Activity {
ListView lst;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_activity);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
ArrayList<SearchResult> results = new ArrayList<SearchResult>();
Iterator<SearchResult> it = results.iterator();
SearchYoutube.prettyPrint(it, query);
lst = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, SearchYoutube.ytstuff);
lst.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, 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);
}
}
In your searchable.xml file in the xml folder, the label cannot be hardcoded. It should be like #string/name or search dialogue wont show.
You need to implement your search activity.
public class SearchableActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SEARCH", "HERE");
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// call the appropriate detail activity
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
Toast.makeText(getApplicationContext(), queryStr, Toast.LENGTH_LONG).show();
}
}
And specify the meta-data in your AndroidManifest.xml
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity
android:name=".SearchableActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Related
Here is my menu class
package devgan.jatin.com.myapplication;
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 {
private final String[] classes={"StartingPoint","GFX"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<>(Menu.this,android.R.layout.simple_list_item_1,classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String cheese=classes[position];
try {
Class ourClass = Class.forName("com.jatin.devgan."+cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
This is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StartingPoint">
<intent-filter>
<action android:name="com.jatin.devgan.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Menu">
<intent-filter>
<action android:name="com.jatin.devgan.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Here's what I did. Created a splash screen which goes to the menu.
You can see the menu class up. When i intend to start an activity of StartingPoint through the menu, its gives me ClassNotFoundException-
but the thing is when i tweaked my code to go straight from splash screen to StartingPoint, it works fine! I'm baffled.
All software is up to date.
why don't you just implement a switch method, to start the activity based on the switch ?
ListView lv = findViewById(R.id.LV);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,int position, long l) {
switch (position)
{
case 1 :
{
Intent intent = new Intent(getApplicationContext(),OtherClass.class);
startActivity(intent);
}break;
case 2:
{
Intent intent = new Intent(getApplicationContext(),OtherClass.class);
startActivity(intent);
}
break;
}
}
});
I have created watchface for android wear and my code is as follows:
ComapnionConfigActivity (in mobile module):
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;
public class CompanionConfigActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companion_config);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
#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_watch_config, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
Manifest for mobile module:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/square"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CompanionConfigActivity"
android:label="#string/title_activity_watch_config" >
<intent-filter>
<action android:name="com.example.agile.mywatchface.MAIN" />
<category android:name= "com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MyWatchfaceService: (service in wear module for drawing watchface)
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.view.SurfaceHolder;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* Created by agile on 3/5/2015.
*/
public class MyWatchfaceService extends CanvasWatchFaceService{
Calendar c;
int hrs,min,sec,width,height;
Timer timer;
TimerTask timerTask;
Paint paint;
#Override
public Engine onCreateEngine() {
return new MyEngine();
}
class MyEngine extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
timer = new Timer();
timerTask = new MyTimerTask();
c = Calendar.getInstance();
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(18);
}
#Override
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
}
#Override
public void onTimeTick() {
super.onTimeTick();
}
#Override
public void onDraw(Canvas canvas, Rect bounds) {
updateTimeOnEachSecond();
width = bounds.width();
height = bounds.height();
canvas.drawText(String.valueOf(hrs)+":"+String.valueOf(min)+":"+String.valueOf(sec),width/2,height/2,paint);
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
}
}
public void updateTimeOnEachSecond() {
timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
class MyTimerTask extends TimerTask {
public void run(){
hrs = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
}
}
}
Manifest for wear module:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/square"
android:label="#string/app_name"
>
<service
android:name=".MyWatchfaceService"
android:label="#string/app_name"
android:permission="android.permission.BIND_WALLPAPER"
android:allowEmbedded="true"
>
<!-- companion configuration activity -->
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/watch_face" />
<meta-data
android:name="com.example.agile.mywatchface.preview"
android:resource="#drawable/square" />
<meta-data
android:name="com.example.agile.mywatchface.preview_circular"
android:resource="#drawable/square" />
<meta-data
android:name="com.google.android.wearable.watchface.companionConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<!-- wearable configuration activity -->
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />
</intent-filter>
</service>
<activity
android:name=".WearConfigActivity"
android:label="#string/title_activity_wear_config"
android:allowEmbedded="true">
<intent-filter>
<action android:name="com.example.agile.mywatchface.CONFIG_DIGITAL" />
<category android:name=
"com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
</activity>
</application>
</manifest>
I have followed official guide for creating watchface. But, when I install the app, it isn't showing in companion app. It seems like the problem is in manifest or companionconfigactivity. I tried for more than a day but couldn't identify it. Can anyone help me out?
In your Wearable Manifest you have following line:
<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />
This is incorrect, you need to use
<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
Android Wear looks for services with this category in the filter, it will ignore yours if you use custom category.
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>
I am self learner of android and i am just trying to learn it with how i can deal with two activities and 3 and so on , for that i just make the sample code , it run fine with the one activity but when i run it for two activities it show me force stop error , i also mention second activity in my manifest.xml
manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.predictor.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.predictor.activity2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Here is mainactivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View btn_activity_1 = findViewById(R.id.btn_activity_1);
btn_activity_1.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_activity_1:
Intent k = new Intent(MainActivity.this, activity2.class);
startActivity(k);
//finish();
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.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);
}
}
You should change :
<category android:name="android.intent.category.LAUNCHER" />
as
<category android:name="android.intent.category.DEFAULT" />
in second activity in xml .
I think I've seen your error: you are casting this to OnClickListener, but your class is not implementing the OnClickListener interface. You should have something like:
public class MainActivity extends ActionBarActivity implements OnClickListener {
// Your code for this class
}
It's difficult to say if it's the only problem, you should post your logs.
I follow this tutorial and i follow all the steps but when i start the app on my device it says it crashed.So i want to make the MainActivity show first and then if i click a Action bar menu, it will load the second activity (upload.java) ant it will show the upload_layout.I'm training making app for android so i'm not good to program ;)
HERE MY CODE:
MainActivity.java
package com.example.tosseapp.app;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.*;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.IOException;
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.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);
}
public void tosse1(View v) {
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.toxxe);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse2(View v) {
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tozze);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse3(View v) {
Button two = (Button)this.findViewById(R.id.button3);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossesei);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse4(View v) {
Button two = (Button)this.findViewById(R.id.button4);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.todde);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse5(View v) {
Button two = (Button)this.findViewById(R.id.button5);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossequattro);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse6(View v) {
Button two = (Button)this.findViewById(R.id.button6);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossecinque);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void upload(View view) {
Intent intent = new Intent(this, upload.class);
startActivity(intent);
}
}
upload.java:
package com.example.tosseapp.app;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class upload extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.upload, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tosseapp.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity
android:name="com.example.tosseapp.app.upload"
android:label="#string/app_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml (menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tosseapp.app.MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:onClick="upload"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You need to declare your MainActivity in your AndroidManifest.xml
<application
android:allowBackup="true"
<!-- other things -->
<activity
android:name="com.example.tosseapp.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- other activities, services, receivers & close application -->
You may need to add both your MainActivity and upload.java into your manifest, something like this:
<application
...
<activity
android:name="com.example.tosseapp.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tosseapp.app.upload"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.tosseapp.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
</activity>
</application>