i'm developing a simple application on android studio. I'm using "application", "observable", and many more. i got some error like this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{id.wdharmana.doahindu/id.wdharmana.doahindu.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to id.wdharmana.doahindu.app.DoaApplication
2nd error
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to id.wdharmana.doahindu.app.DoaApplication
3rd error
at id.wdharmana.doahindu.MainActivity.onCreate(MainActivity.java:52)
This is my full MainActivity.java:
package id.wdharmana.doahindu;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import id.wdharmana.doahindu.adapter.ListJudulAdapter;
import id.wdharmana.doahindu.app.DoaApplication;
import id.wdharmana.doahindu.data.DefaultData;
import id.wdharmana.doahindu.helper.DoaHelper;
import id.wdharmana.doahindu.model.DoaModel;
import id.wdharmana.doahindu.model.DoaObserver;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
public class MainActivity extends AppCompatActivity implements Observer {
private ListView lvJudul;
private ArrayList<DoaModel> listJudul;
private DoaHelper doaHelper;
public ListJudulAdapter listJudulAdapter;
private DoaApplication application;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvJudul = (ListView)findViewById(R.id.lvListJudul);
listJudul = new ArrayList<DoaModel>();
application = (DoaApplication) getApplication();
application.getDoaObserver().addObserver(this);
doaHelper = new DoaHelper(MainActivity.this);
doaHelper.open();
listJudul = doaHelper.getAllData();
if (listJudul.size()>0) {
bindData();
}else{
insertDefaultData();
}
lvJudul.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
showMeaningDialog(MainActivity.this, listJudul.get(arg2));
}
});
lvJudul.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
deleteDialog(listJudul.get(arg2).getId());
return false;
}
});
}
private void insertDefaultData() {
// TODO Auto-generated method stub
new StoreDefaultData().execute();
}
public void update(Observable observable, Object o) {
if (o.equals(DoaObserver.NEED_TO_REFRESH)){
bindData();
}
}
private class StoreDefaultData extends AsyncTask<Void, Void, Void>{
ProgressDialog mProgressDialog;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle(getString(R.string.notify_input_data));
mProgressDialog.setMessage(getString(R.string.text_please_wait));
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for (int i = 0; i < DefaultData.defaultData.length; i++) {
doaHelper.insert(DoaModel.getDoaModel(DefaultData.defaultData[i][0],
DefaultData.defaultData[i][1]));
}
listJudul = doaHelper.getAllData();
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
listJudulAdapter = new ListJudulAdapter(MainActivity.this, listJudul);
lvJudul.setAdapter(listJudulAdapter);
}
}
#Override
protected void onDestroy() {
if (doaHelper != null){
doaHelper.close();
}
super.onDestroy();
}
public static void showMeaningDialog(final Activity activity, final DoaModel item) {
final Dialog dialog = new Dialog(activity, R.style.AppCompatAlertDialogStyle);
dialog.setContentView(R.layout.dialog_konten);
dialog.setCancelable(true);
TextView txtKonten = (TextView)dialog.findViewById(R.id.txtMeaning);
TextView txtJudul = (TextView)dialog.findViewById(R.id.txtWord);
Button btnTutup = (Button)dialog.findViewById(R.id.btnTutup);
Button btnEdit = (Button)dialog.findViewById(R.id.btnEdit);
txtKonten.setText(item.getKonten());
txtJudul.setText(item.getJudul());
btnEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//FormInputUpdateActivity.toFormInputUpdate(activity, item);
dialog.dismiss();
}
});
btnTutup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
private void deleteDialog(final int id) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this, R.style.AppCompatAlertDialogStyle);
dialog.setTitle("Hapus");
//dialog.setContentView(R.layout.dialog_delete);
dialog.setCancelable(true);
// Button btnYes = (Button)dialog.findViewById(R.id.btnDeleteYes);
// Button btnCancel = (Button)dialog.findViewById(R.id.btnDeleteCancel);
// btnYes.setOnClickListener(new OnClickListener() {
// public void onClick(View v) {
// // TODO Auto-generated method stub
// doaHelper.delete(id);
// dialog.dismiss();
// Toast.makeText(MainActivity.this, getString(R.string.text_success_delete), Toast.LENGTH_LONG).show();
// application.getDoaObserver().refresh();
// }
// });
//btnCancel.setOnClickListener(new View.OnClickListener() {
// public void onClick(View arg0) {
// TODO Auto-generated method stub
// dialog.dismiss();
// }
//});
dialog.show();
}
public void bindData(){
if (listJudul.size()>0) {
listJudul.clear();
}
listJudul = doaHelper.getAllData();
listJudulAdapter = new ListJudulAdapter(MainActivity.this, listJudul);
lvJudul.setAdapter(listJudulAdapter);
listJudulAdapter.notifyDataSetChanged();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
This is DoaApplication.java
package id.wdharmana.doahindu.app;
import android.app.Application;
import id.wdharmana.doahindu.model.DoaObserver;
/**
* Created by WDHARMANA on 9/18/2015.
*/
public class DoaApplication extends Application {
DoaObserver doaObserver;
#Override
public void onCreate() {
super.onCreate();
doaObserver = new DoaObserver();
}
public DoaObserver getDoaObserver(){
return doaObserver;
}
}
No error when build. Please tell me if you have some suggestions. Thanks in advance.
Put DoaApplication in your manifest, in the <application> node as android:name="id.wdharmana.doahindu.app.DoaApplication"
Please make sure your AndroidManifest.xml like that:
<application
android:name="id.wdharmana.doahindu.app.DoaApplication"
... >
...
</application>
I think you forgot to add your
The baseactivity of this code display admob interstial ads and refresh after five minutes but how to edit this code so that Interstial ads display only at the launching time, no refreshing of the interstial ads. The interstial ads need to display only at the app launching time, where to edit the code.
package com.samplecodes.app;
import java.io.File;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.samplecodes.app.R;
import com.samplecodes.app.constant.ConstantVariables;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class BaseActivity extends Activity implements OnClickListener {
public SharedPreferences mPrefs;
SharedPreferences.Editor editor;
AdView adView;
String AD_UNIT_ID = "";
private InterstitialAd interstitialAds = null;
private int postdelay = 5;
private Handler myHandler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.layout_header);
mPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
int old_versioncode = mPrefs.getInt(ConstantVariables.VERSION_CODE, 0);
int v = 0;
try {
v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
if (v != old_versioncode) {
// Delete old database file
File dbFile = getDatabasePath(ConstantVariables.DATABASE_NAME);
dbFile.deleteOnExit();
editor = mPrefs.edit();
editor.putInt(ConstantVariables.VERSION_CODE, v);
editor.commit();
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
this.setupInterstitialAds();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.sharebtn:
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare
.putExtra(
android.content.Intent.EXTRA_TEXT,
getString(R.string.app_name)
+ " /r/n "
+ "https://play.google.com/store/apps/details?id=com.samplecodes.app");
startActivity(Intent.createChooser(intentShare, "Share"));
break;
default:
break;
}
}
protected void setupInterstitialAds() {
// Create an ad.
this.interstitialAds = new InterstitialAd(this);
this.interstitialAds.setAdUnitId("");// AD_UNIT_ID
// Set the AdListener.
this.interstitialAds.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
myHandler = new Handler();
myHandler.postDelayed(runRepeatIntertitialAds, postdelay * 1000*60);
}
#Override
public void onAdFailedToLoad(int errorCode) {
Log.e("aaaaaaaaaaaaaaa",
"Interstitial ad was not ready to be shown. onAdFailedToLoad "
+ errorCode);
}
});
final TelephonyManager tm = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(deviceid).build();
this.interstitialAds.loadAd(adRequest);
}
private Runnable runRepeatIntertitialAds = new Runnable()
{
public void run()
{
showInterstitial();
//write here whaterver you want to repeat
myHandler.postDelayed(this, postdelay * 1000*60);
}
};
protected void setupAdmob() {
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
LinearLayout layout = (LinearLayout) findViewById(R.id.rootLayout);
layout.addView(adView);
final TelephonyManager tm = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = tm.getDeviceId();
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(deviceid).build();
// AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
protected void share() {
ImageView shareImg = (ImageView) findViewById(R.id.sharebtn);
shareImg.setOnClickListener(this);
}
/** Called when the Show Interstitial button is clicked. */
public void showInterstitial() {
if (this.interstitialAds.isLoaded()) {
this.interstitialAds.show();
} else {
Log.e("", "Interstitial ad was not ready to be shown.");
}
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (myHandler != null) {
myHandler.removeCallbacks(runRepeatIntertitialAds);
}
}
}
Change #onAdLoaded to be:
private boolean alreadyShownInterstitial;
#Override
public void onAdLoaded() {
if (!alreadyShownInterstitial) {
alreadyShownInterstitial = true;
showInterstitial();
}
}
Just note that Google doesn't take kindly to out of flow display of interstitial ads like this, and you may well get your account banned. You are much better off displaying the interstitial at a natural break point in your app.
I was trying to run some test on SurfaceViewer with SurfaceViewer having its own thread and everything worked just fine. Then I changed my code to change orientation of screen using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
In the code below: I have an activity which uses Fragments. I have another class which extends SurfaceView class. Problem starts when I add setRequestedOrientation. After I add setRequestedOrientation. Value of running at in FastRender Class run method is always false. If I comment line with setRequestedOrientation , it runs fines.
package com.example.testpractise;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
public class SurfaceViewTest extends ActionBarActivity {
static int counter;
static {
Log.i("Intializing class Surface Test","counter");
System.out.println("Statrting here");
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
counter++;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
counter++;
Log.i("Counter vlase",String.valueOf(counter));
System.out.println("value of counter"+counter);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_surface_view_test);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.surface_view_test, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
FastRenderView renderView;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View rootView = inflater.inflate(
// R.layout.fragment_surface_view_test, container, false);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
BitmapCollection.initializeBitMapCollection(getActivity());
renderView = new FastRenderView(getActivity());
return renderView;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
renderView.resume();
}
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
renderView.pause();
}
static class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
static volatile boolean running = false;
int x=0;
int y=0;
public FastRenderView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void pause()
{
running = false;
while(true)
{
try{
renderThread.join();
}catch (InterruptedException i){
}
}
}
public void resume()
{
running = true;
renderThread = new Thread(this);
renderThread.setName("RendererThread");
renderThread.start();
running=true;
}
#Override
public void run() {
// TODO Auto-generated method stub
int sleepCounter=0;
while(!running)
{
try {
if(sleepCounter <5)
{
sleepCounter++;
Thread.sleep(5000);
}else{
break;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(running)
{
if(!holder.getSurface().isValid())
{
continue;
}
Canvas canvas=holder.lockCanvas();
canvas.drawRGB(255, 0, 0);
holder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Sorry if my question doesn't match the format. This is my first time. Any pointers on what is wrong and why would be greatly appreciated.
Thanks
If you just want to lock the Activity to landscape, you can do it in Manifest by adding the android:screenOrientation="landscape" attribute to the activity element.
I need to permanently disable the Ads while changing to landscape mode in android app.Below I listed the codes:
PlayerActivity.java:
package com.grace.view;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import com.grace.view.ads.Ads;
public class PlayerActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener, AdListener{
// create string variables
String YOUTUBE_APIKEY;
String ID;
private InterstitialAd interstitial;
private static final String LOG_TAG = "LayarTancep";
// create object of view
YouTubePlayerView youTubePlayerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
// connect view object and view id on xml
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
// create the interstitial
interstitial = new InterstitialAd(this, getString(R.string.admob_id));
// load interstitialAd
Ads.loadInterstitialAd(interstitial);
// set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
// get YOUTUBE APIKEY
YOUTUBE_APIKEY = getString(R.string.youtube_apikey);
// get video id from previous page
Intent i = getIntent();
ID = i.getStringExtra("id");
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
if(result == YouTubeInitializationResult.DEVELOPER_KEY_INVALID)
Toast.makeText(this, "Initialization Fail- key invalid", Toast.LENGTH_SHORT).show();
else if(result == YouTubeInitializationResult.NETWORK_ERROR)
Toast.makeText(this, getString(R.string.no_connection), Toast.LENGTH_SHORT).show();
else if(result == YouTubeInitializationResult.SERVICE_INVALID)
updateYoutubeDialog(
getString(R.string.update_youtube_app),
getString(R.string.update));
else if(result == YouTubeInitializationResult.SERVICE_MISSING)
updateYoutubeDialog(
getString(R.string.no_youtube_app),
getString(R.string.install));
}
void updateYoutubeDialog(String message, String button){
//if Youtube app is not available show alert dialog
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.youtube"));
startActivity(intent);
//Finish the activity so they can't circumvent the check
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.loadVideo(ID);
}
}
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onDismissScreen");
// display youtube player
youTubePlayerView.initialize(YOUTUBE_APIKEY, this);
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
String message = "onFailedToReceiveAd (" + arg1 + ")";
Log.d(LOG_TAG, message);
// display youtube player
youTubePlayerView.initialize(YOUTUBE_APIKEY, this);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onLeaveApplication");
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d(LOG_TAG, "onPresentScreen");
}
#Override
public void onReceiveAd(Ad ad) {
// TODO Auto-generated method stub
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
if I click any one of the particular video,the ads would be
occurred.After I cancel the ads,the video will be playing.
I need to disable the ads permanently.Is there any way to solve
it.Thank You.
When you want to change the orientation configuration you need to find the same activity created again for orientation changes, here you will realize Android Life Cycle.
So this one will help you
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/link
I am making my first Android app and I am trying to implement Leadbolt unlocker, I have done everything like Leadbolt documentation, but when I launch app it is always crashing. When I remove implement AdListener from public class FullActivity extends Activity implements AdListener then everything is working. App is crashing when Intent full = new Intent(GridActivity.this, FullActivity.class); and it is not even go to super.onCreate(savedInstanceState); breakpoint.
This is FullActivity.java:
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.Leadbolt.AdController;
import com.Leadbolt.AdListener;
public class FullActivity extends Activity implements AdListener{
private AdController myController;
private String MY_LB_SECTION_ID="1111111";
private Integer[] mThumbIds = {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6,
R.drawable.p7,
R.drawable.p8,
R.drawable.p9,
R.drawable.p10,
R.drawable.p11,
R.drawable.p12,
R.drawable.p13,
R.drawable.p14,
R.drawable.p15,
R.drawable.p16,
R.drawable.p17,
R.drawable.p18,
R.drawable.p19,
R.drawable.p20,
R.drawable.p21,
R.drawable.p22,
R.drawable.p23,
R.drawable.p24,
R.drawable.p25,
R.drawable.p26,
R.drawable.p27,
R.drawable.p28,
R.drawable.p29,
R.drawable.p30,
R.drawable.p31,
R.drawable.p32,
R.drawable.p33,
R.drawable.p34,
R.drawable.p35,
R.drawable.p36,
R.drawable.p37,
R.drawable.p38,
R.drawable.p39,
R.drawable.p40,
R.drawable.p41,
R.drawable.p42,
R.drawable.p43,
R.drawable.p44,
R.drawable.p45,
R.drawable.p46,
R.drawable.p47,
R.drawable.p48,
R.drawable.p49,
R.drawable.p50
};
Integer imageId;
TouchImageView touch;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
final Activity act = FullActivity.this;
final AdListener listener = FullActivity.this;
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
imageId = extras.getInt("image");
touch = new TouchImageView(this);
Bitmap snoop = BitmapFactory.decodeResource(getResources(), mThumbIds[imageId]);
touch.setImageBitmap(snoop);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f
setContentView(touch);
}
final String PREFS_NAME = "AppPrefs";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int ad = settings.getInt("ad", 0);
if((imageId > 25)&&(ad < 1)){
touch.post(new Runnable() {
public void run(){
myController = new AdController(act, MY_LB_SECTION_ID,
listener);
myController.loadAd();
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.set_back:
setBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setBack() {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(mThumbIds[imageId]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(FullActivity.this.getApplicationContext(), "Wallapers set!", Toast.LENGTH_SHORT).show();
}
public void onDestroy(){
myController.destroyAd();
super.onDestroy();
}
public void onAdClicked() {}
public void onAdClosed() {}
public void onAdCompleted() {
// TODO Auto-generated method stub
SharedPreferences settings = getSharedPreferences("AppPrefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ad", 1);
// Commit the edits!
editor.commit();
}
public void onAdFailed() {
this.runOnUiThread(new Runnable() {
public void run() {
if(myController != null){
myController.destroyAd();
}
}
});
}
public void onAdLoaded() {}
public void onAdProgress() {}
}
We have made some adjustments to the code which should resolve this for you.
package com.mkstudio.hdwallpapers;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.Leadbolt.AdController;
import com.Leadbolt.AdListener;
public class FullActivity extends Activity{
private AdController myController;
private String MY_LB_SECTION_ID="729926945";
// references to our images
private Integer[] mThumbIds = {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6,
R.drawable.p7,
R.drawable.p8,
R.drawable.p9,
R.drawable.p10,
R.drawable.p11,
R.drawable.p12,
R.drawable.p13,
R.drawable.p14,
R.drawable.p15,
R.drawable.p16,
R.drawable.p17,
R.drawable.p18,
R.drawable.p19,
R.drawable.p20,
R.drawable.p21,
R.drawable.p22,
R.drawable.p23,
R.drawable.p24,
R.drawable.p25,
R.drawable.p26,
R.drawable.p27,
R.drawable.p28,
R.drawable.p29,
R.drawable.p30,
R.drawable.p31,
R.drawable.p32,
R.drawable.p33,
R.drawable.p34,
R.drawable.p35,
R.drawable.p36,
R.drawable.p37,
R.drawable.p38,
R.drawable.p39,
R.drawable.p40,
R.drawable.p41,
R.drawable.p42,
R.drawable.p43,
R.drawable.p44,
R.drawable.p45,
R.drawable.p46,
R.drawable.p47,
R.drawable.p48,
R.drawable.p49,
R.drawable.p50
};
Integer imageId;
TouchImageView touch;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.full);
Bundle extras = getIntent().getExtras();
if (extras != null) {
imageId = extras.getInt("image");
touch = new TouchImageView(this);
Bitmap snoop = BitmapFactory.decodeResource(getResources(), mThumbIds[imageId]);
touch.setImageBitmap(snoop);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f
setContentView(touch);
}
final String PREFS_NAME = "SexyRec";
final Activity act = this;
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int ad = settings.getInt("ad", 0);
if((imageId > 25)&&(ad < 1)){
touch.post(new Runnable() {
public void run(){
myController = new AdController(act, MY_LB_SECTION_ID, new AdListener() {
public void onAdProgress() {
// TODO Auto-generated method stub
}
public void onAdLoaded() {
// TODO Auto-generated method stub
}
public void onAdFailed() {
// TODO Auto-generated method stub
}
public void onAdCompleted() {
// TODO Auto-generated method stub
SharedPreferences settings = getSharedPreferences("SexyRec", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ad", 1);
// Commit the edits!
editor.commit();
}
public void onAdClosed() {
// TODO Auto-generated method stub
}
public void onAdClicked() {
// TODO Auto-generated method stub
}
});
myController.loadAd();
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.set_back:
setBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setBack() {
//chuckNorris.setResourceAsWallpaper(mThumbIds[imageId]);
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(mThumbIds[imageId]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(FullActivity.this.getApplicationContext(), "Wallapers set!", Toast.LENGTH_SHORT).show();
}
public void onDestroy(){
myController.destroyAd();
super.onDestroy();
}
}