Android App crash with two activity - java

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>

Related

Android button not opening other class

I have problems with buttons.I have made 2 buttons that will when pressed open other activity(class).In code there is no error but when I launch emulator it just doesn't work and isn't opening those classes.
My button's btnopis and btnpronadi don't work(they are not opening other activity(class)).But I have button exit that Works and closes app. I don't get where the problem is. Here is the code
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
izlaz();
}
public Button btnopis;
public void otvoriopis(){
btnopis=(Button)findViewById(R.id.btnopis);
btnopis.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent otvoriopis= new Intent(MainActivity.this,Opis.class);
startActivity(otvoriopis);
}
});
}
public Button btnpronadi;
public void otvoripronadi(){
btnpronadi=(Button)findViewById(R.id.btnpronadi);
btnpronadi.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Intent otvoripronadi= new Intent(MainActivity.this,Pronadi.class);
startActivity(otvoripronadi);
}
});
}
public Button btnizlaz;
public void izlaz(){
btnizlaz=(Button)findViewById(R.id.btnizlaz);
btnizlaz.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
here is the manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shromid">
<application
android:allowBackup="true"
android:icon="#mipmap/app_ikona"
android:roundIcon="#mipmap/app_icon_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreen"
android:label="ShromID"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity android:name=".Opis" />
<activity android:name=".Pronadi"></activity>
</application>
</manifest>
You forgot do call otvoriopis() and otvoripronadi() at onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
izlaz();
otvoriopis();
otvoripronadi();
}
You should call your functions in onCreate method, like you did for 'izlaz'

Second activity not getting started after splash screen

After the splash screen following error displayed. How to solve this?
This error message is displayed after completing the specified time in thread in SplashScr.java
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lalinda.splashscreen">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScr"
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.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Splash"/>
</application>
</manifest>
SplashScr.java
package com.example.lalinda.splashscreen;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Lalinda on 8/7/2016.
*/
public class SplashScr extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread myThread = new Thread()
{
#Override
public void run()
{
try {
sleep(10000);
Intent startMainScreen = new Intent(getApplicationContext(), Splash.class);
startActivity(startMainScreen);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
Splash.java : This is the second activity
package com.example.lalinda.splashscreen;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
#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_splash, 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);
}
}
Edit : logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lalinda.splashscreen/com.example.lalinda.splashscreen.Splash}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
You need to remove the ActionBar, before setting a toolbar. Set your app theme something like:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
Like #Habel Philip mentioned in his comment above, use a Handler to show the Splash screen for a limited time. In fact, I would suggest against using a Splash screen in the first place. This is the recommended pattern now (Launch Screen Pattern): https://www.bignerdranch.com/blog/splash-screens-the-right-way/

WatchFaces are not configuring

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.

Search Dialogue not activated when onSearchRequested is called (Android)

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>

Fortumo In-App payment for Android

I'm trying to create a application, where you can buy In-App credits with the Fortumo service. I've managed to get the makePayment(); method working so I can make a payment and everything seems to work. The only thing is, that it looks like the onReceive(); is never called because the credits count is not changed after a successful payment. The credits count should be increased by 32000 with one payment of 0.32€.
Here is the main activity java:
package com.your.raha;
import com.your.raha.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
static int counter;
static TextView display;
SharedPreferences someData;
public static String filename = "rahafail";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.proov);
display = (TextView) findViewById(R.id.tvKonto);
someData = getSharedPreferences(filename, 0);
int returnedInt = someData.getInt("sharedInt", 0);
counter = returnedInt;
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
Button SoundButton1 = (Button)findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
AddOne();
}
});
Button bOsta = (Button)findViewById(R.id.bOsta);
bOsta.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onStop();
startActivity(new Intent("com.uugudev.rahaboss.FORTUMOMAKSA"));
}
});
ImageButton Abi = (ImageButton)findViewById(R.id.ibAbi);
Abi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
counter = 0;
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
display.setText(counter + " EEK");
}
});
}
public static void AddOne() {
// TODO Auto-generated method stub
counter++;
display.setText(counter + " EEK");
}
public static void Osteti() {
// TODO Auto-generated method stub
counter = counter + 32000;
display.setText(counter + " EEK");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
int returnedInt = someData.getInt("sharedInt", 0);
counter = returnedInt;
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
display.setText(counter + " EEK");
}
}
The payment activity class:
package com.your.raha;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import com.fortumo.android.Fortumo;
import com.fortumo.android.PaymentActivity;
import com.fortumo.android.PaymentRequestBuilder;
import com.fortumo.android.PaymentResponse;
public class FortumoMaksa extends PaymentActivity {
/** Called when the activity is first created. */
public int raha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Fortumo.enablePaymentBroadcast(this, Manifest.permission.PAYMENT_BROADCAST_PERMISSION);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setDisplayString("Donate");
builder.setProductName("stars_myawesomeusername");
makePayment(builder.build());
}
;
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPaymentCanceled(PaymentResponse response) {
Toast.makeText(this, "Kasutaja katkestas makse", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentFailed(PaymentResponse response) {
Toast.makeText(this, "Makse ebaõnnestus", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentPending(PaymentResponse response) {
Toast.makeText(this, "Makse ootel", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentSuccess(PaymentResponse response) {
Toast.makeText(this, "Makse õnnestus", Toast.LENGTH_SHORT).show();
finish();
}
}
The PaymentStatusReceived class:
package com.your.raha;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.fortumo.android.Fortumo;
public class PaymentStatusReceiver extends BroadcastReceiver {
private static String TAG = "PaymentStatusReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
int billingStatus = extras.getInt("billing_status");
if(billingStatus == Fortumo.MESSAGE_STATUS_BILLED) {
Soundboard.Osteti();
}
}
}
The AndroidManifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your.raha"
android:installLocation="auto"
android:versionCode="2"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<permission
android:name="com.your.domain.PAYMENT_BROADCAST_PERMISSION"
android:label="Read Fortumo payment status"
android:protectionLevel="signature" />
<uses-permission android:name="com.your.raha.PAYMENT_BROADCAST_PERMISSION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="com.fortumo.android.BillingSMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name="com.fortumo.android.FortumoService" />
<service android:name="com.fortumo.android.StatusUpdateService" />
<activity
android:name="com.fortumo.android.FortumoActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<receiver
android:name=".PaymentStatusReceiver"
android:permission="com.your.raha.PAYMENT_BROADCAST_PERMISSION" >
<intent-filter>
<action android:name="com.fortumo.android.PAYMENT_STATUS_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.fortumo.android.FortumoActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".PaymentStatusReceiver"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.uugudev.rahaboss.PAYMENTSTATUSRECEIVER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity
android:name="com.fortumo.android.FortumoActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<service android:name="com.fortumo.android.FortumoService" />
<activity
android:name=".Soundboard"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".FortumoMaksa"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.uugudev.rahaboss.FORTUMOMAKSA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
So basically I'm trying to start the Soundboard.Osteti(); in the Soundboard.java that adds 32000 to the counter int and then updates the TextView. But nothing happens.
Hope somebody can help me. Thanks!
I'm still trying to make this work.
There is this thing I think you've wrong in your manifest:
PaymentStatusReceiver it's not an activity, and you only need to configure that as a receveiver (as you already had).

Categories