how to make my background to flash constantly with animation - java

My code is as below but.. when i ran it, it doesn't do anything
Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
// the
// end so the layout will
// fade back in
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
x.clearAnimation();
logcat after vipul mittal suggest
12-11 20:08:18.477: E/AndroidRuntime(1571): FATAL EXCEPTION: main
12-11 20:08:18.477: E/AndroidRuntime(1571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NTS.standroid/com.NTS.standroid.Settings}: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.os.Looper.loop(Looper.java:130)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 20:08:18.477: E/AndroidRuntime(1571): at dalvik.system.NativeStart.main(Native Method)
12-11 20:08:18.477: E/AndroidRuntime(1571): Caused by: java.lang.NullPointerException
12-11 20:08:18.477: E/AndroidRuntime(1571): at com.NTS.standroid.Settings.onCreate(Settings.java:35)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 20:08:18.477: E/AndroidRuntime(1571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-11 20:08:18.477: E/AndroidRuntime(1571): ... 11 more
any suggestions please.
I want to make my background to constantly flash after i hit a button
My current code
}
#SuppressLint("NewApi")
public void tintBackground() {
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
x.setBackgroundDrawable(trans);
} else {
x.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds
}
and xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout="#+id/warning">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="#+id/warning1"
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:background="#drawable/warning"
/>
</LinearLayout>

I use the following code to tint the background of my app, maybe you can use that repeatedly:
#SuppressLint("NewApi")
public void tintBackground() {
View rootView = findViewById(android.R.id.content);
ColorDrawable[] color = { new ColorDrawable(Color.RED),
new ColorDrawable(Color.WHITE) };
TransitionDrawable trans = new TransitionDrawable(color);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rootView.setBackgroundDrawable(trans);
} else {
rootView.setBackground(trans);
}
trans.startTransition(2000); // do transition over 2 seconds
}

You must start animation on the linearlayout:
Animation animation = new AlphaAnimation(1, 0); // Change alpha
// from fully
// visible to
// invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
// animation
// rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
// infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at
// the
// end so the layout will
// fade back in
LinearLayout x = (LinearLayout)findViewById(R.id.warning);
x.startAnimation(animation)//<-- start animation don't clear it

I have used this to make a View fade in and out repeatedly:
fadeIn = new AlphaAnimation(0.25f, 1);
fadeIn.setInterpolator(new AccelerateInterpolator()); // add this
fadeIn.setDuration(500);
fadeIn.setFillAfter(true);
fadeIn.setAnimationListener(new RepeatAnimationListener());
fadeOut = new AlphaAnimation(1, 0.25f);
fadeOut.setInterpolator(new DecelerateInterpolator()); // and this
fadeOut.setDuration(500);
fadeOut.setFillAfter(true);
fadeOut.setAnimationListener(new RepeatAnimationListener());
private boolean currently_fadeOut;
private class RepeatAnimationListener implements AnimationListener {
public void onAnimationEnd(Animation animation) {
if (currently_fadeOut) {
view.startAnimation(fadeIn);
currently_fadeOut = false;
} else {
view.startAnimation(fadeOut);
currently_fadeOut = true;
}
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
// automatically repeated because of RepeatAnimationListener
view.startAnimation(fadeOut);

Related

Create Popup with Runnable Thread Android

i m trying to create popup with runnable but i m not able to. i always get "NULLPOINTEREXPEXTIONS" i m almost done with my android app, the only thing left is show popup after some time, i used runnable to show popup
here is my code, ERROR generate this line
View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element));
NOTE : popup_element is ID of popup layout xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
public void run() {
//Do something after 100ms
initiatePopupWindow();
}
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
}, 700);
}
}
ERROR LOG
D/gralloc_goldfish(906): Emulator without GPU emulation detected.
W/System.err(906): java.lang.NullPointerException
W/System.err(906): at com.example.runnable_with_popup.MainActivity$1.initiatePopupWindow(MainActivity.java:43)
W/System.err(906): at com.example.runnable_with_popup.MainActivity$1.run(MainActivity.java:34)
W/System.err(906): at android.os.Handler.handleCallback(Handler.java:730)
W/System.err(906): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(906): at android.os.Looper.loop(Looper.java:137)
W/System.err(906): at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err(906): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(906): at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err(906): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err(906): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err(906): at dalvik.system.NativeStart.main(Native Method)
Please help me out from this problem. i searched over internet but did not found example or question related to my problem
help will be appreciated
thanks
The parent might be missing, put false on the last part
View layout = inflater.inflate(R.layout.popup_layout,(ViewGroup) findViewById(R.id.popup_element),false);
use this one
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);

App crashes when images load

so Im trying to make an app that when you click on a button, it displays a lot of images. The problem is that when I click the button, the application crashes.
Here is a part of my XML file. It has 36 different images:
<ImageView
android:id="#+id/imageView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh1" />
<ImageView
android:id="#+id/imageView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh2" />
<ImageView
android:id="#+id/imageView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/veh3" />
...
And here is the LogCat:
02-18 22:47:00.263: E/AndroidRuntime(366): FATAL EXCEPTION: main
02-18 22:47:00.263: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.guzi.samphelptools/com.guzi.samphelptools.Vehicles}: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.os.Looper.loop(Looper.java:130)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 22:47:00.263: E/AndroidRuntime(366): at dalvik.system.NativeStart.main(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: android.view.InflateException: Binary XML file line #81: Error inflating class <unknown>
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Activity.setContentView(Activity.java:1657)
02-18 22:47:00.263: E/AndroidRuntime(366): at com.guzi.samphelptools.Vehicles.onCreate(Vehicles.java:14)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 11 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.reflect.InvocationTargetException
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.constructNative(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 24 more
02-18 22:47:00.263: E/AndroidRuntime(366): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.Res`enter code here`ources.loadDrawable(Resources.java:1709)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:118)
02-18 22:47:00.263: E/AndroidRuntime(366): at android.widget.ImageView.<init>(ImageView.java:108)
02-18 22:47:00.263: E/AndroidRuntime(366): ... 27 more
Okay, so I found this simple listview thing, but i cant get it running.
import android.os.Bundle;
import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// Array of integers points to images stored in /res/drawable-hdpi/
int[] vehs = new int[]{
R.drawable.veh1,
R.drawable.veh2,
R.drawable.veh3,
R.drawable.veh4,
R.drawable.veh5,
R.drawable.veh6,
R.drawable.veh7,
R.drawable.veh8,
R.drawable.veh9,
R.drawable.veh10,
R.drawable.veh11,
R.drawable.veh12,
R.drawable.veh13,
R.drawable.veh14,
R.drawable.veh15,
R.drawable.veh16,
R.drawable.veh17,
R.drawable.veh18,
R.drawable.veh19,
R.drawable.veh20,
R.drawable.veh21,
R.drawable.veh22,
R.drawable.veh23,
R.drawable.veh24,
R.drawable.veh25,
R.drawable.veh26,
R.drawable.veh27,
R.drawable.veh28,
R.drawable.veh29,
R.drawable.veh30,
R.drawable.veh31,
R.drawable.veh32,
R.drawable.veh33,
R.drawable.veh34,
R.drawable.veh35,
R.drawable.veh36,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("", Integer.toString(vehs[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.vehs};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
If you could help me with this, that would be great! :D
The problem you have here is you are loading everything at the same time, I guess. You should try using a ListView and inflate each row! The way this would work is that your app will load an ImageView or two at once and the rest will be left on the side, until the user scrolls close to it. The tutorial I found for this is (watch #71 to about #87 - you can get all his videos at slidenerd.com):
http://www.youtube.com/watch?v=uic3TVp_j3M#t=0
You should also resize your bitmaps so they aren't loaded in full resolution. You don't need a 1920x1080 image for a 540*420 screen, for example. The following link shows you how to take care of this.
http://developer.android.com/training/displaying-bitmaps/index.html
So, in the end, you will have only 1 ListView, 1 ImageView and way less code and way less confusion :P
EDIT: QUICK GUIDE:
I'll walk you through the ListView method only, because I think the scaling down of an image example by Google is easy to follow.
So, first of all, you will have the following:
myActivity.xml (the main Activity where you need these images)
Single row (the layout of what a single row looks like)
MyActivity.java (The java class for your activity)
myActivity.xml will contain a ListView:
<ListView
android:id="#+id/lvList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
singleRow.xml will contain just an ImageView - you will use the same ImageView's LayoutParamaters and mirror them 36 times:
<ImageView
android:id="#+id/ivImage"
android:layout_width="50dp"
android:layout_height="85dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
MyActivity.java - where all the magic happens!
public class MyActivity extends Activity implements OnItemClickListener {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
lv = (ListView) findViewById(R.id.lvList);
MyAdapter adapter = new MyAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
MyView holder = (MyView) view.getTag(); // This will get the tag of the item click - that is set up below
}
//Create a holder for the image that will be in an ImageView
class SingleRowData {
int img;
SingleRowData(int img) {
this.img = img;
}
}
//A reference to the ImageView that needs to be copied
private class MyView {
ImageView ivImg;
MyView(View v) {
ivImg = (ImageView) v.findViewById(R.id.ivHomePageRow);
}
}
class MyAdapter extends BaseAdapter{
ArrayList<SingleHPSRow> list;
Context context;
public MyAdapter(Context context) {
this.context = context;
list = new ArrayList<SingleRowData>();
...
int[] imgId = ...; // let's say you are getting all the images from your resources .. you will have to set this up, I'm guessing you know how to.
for(int item = 0; item < imgUrl.length; i++) {
list.add(imgId[item]);
}
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view row = convertView; //This will try to see if the row has already been loaded b4
MyView holder = null; initialize our custome View for the row
if(row == null) { // new row
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //Get the LayoutInflater
row = inflater.inflate(R.layout.singleRow, parent, false); //Reference to the row layout
holder = new MyView(row);
row.setTag(holder); // Give the row a tag - the holder that contains the item
} else { // old row
holder = (MyView) row.getTag(); //Get the tag that was assigned
}
SingleRowData item = list.get(position); //Get the item at the current position
int image = item.img; //Get the imgId assigned to the current item
holder.ivImg.setImageResource(image); //Load image for the current position
return row; // Return the current row
}
}
}
Hopefull this is well explained, I copied and shortened one of my codes. Let me know if there's something you don't understand

Admob Google Play services. null pointer exception when showing interstitial ads

I'm trying to integrate admob to my game, but I'm getting nullpointerexcpetion when I'm trying to show interstitial ads. Here is my code..
Inside of onCreate
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("a1528e2f9897fc5");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
I'm getting error on this line.. interstitial.loadAd(adRequest);
and here is my log
12-11 17:12:41.755: E/AndroidRuntime(9357): FATAL EXCEPTION: main
12-11 17:12:41.755: E/AndroidRuntime(9357): java.lang.RuntimeException: Unable to start activity ComponentInfo{cr.logics.fastfood/cr.logics.fastfood.FastFood}: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.os.Looper.loop(Looper.java:137)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-11 17:12:41.755: E/AndroidRuntime(9357): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 17:12:41.755: E/AndroidRuntime(9357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-11 17:12:41.755: E/AndroidRuntime(9357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-11 17:12:41.755: E/AndroidRuntime(9357): at dalvik.system.NativeStart.main(Native Method)
12-11 17:12:41.755: E/AndroidRuntime(9357): Caused by: java.lang.NullPointerException
12-11 17:12:41.755: E/AndroidRuntime(9357): at tj.a(SourceFile:191)
12-11 17:12:41.755: E/AndroidRuntime(9357): at tt.onTransact(SourceFile:81)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.os.Binder.transact(Binder.java:326)
12-11 17:12:41.755: E/AndroidRuntime(9357): at com.google.android.gms.internal.ac$a$a.a(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357): at com.google.android.gms.ads.InterstitialAd.loadAd(Unknown Source)
12-11 17:12:41.755: E/AndroidRuntime(9357): at cr.logics.fastfood.FastFood.onCreate(FastFood.java:245)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.Activity.performCreate(Activity.java:5206)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-11 17:12:41.755: E/AndroidRuntime(9357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-11 17:12:41.755: E/AndroidRuntime(9357): ... 11 more
I have added google-play-services-lib to my project, added meta-data into manifest, did everything as google guide, but I'm facing to this error, (sorry for my beta English).
Any suggestions?
Thanks in advance!
Add the following to your project's proguard-project.txt to prevent it from stripping away the admob classes
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}
And also include the following activity to your manifest file
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Before show interstitial ad you must check interstitial ad object is null or not and also check ad is loaded.
Full code is given below :
In the onCreate() method of an Activity:
public class MainActivity extends Activity {
public static InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdClosed() {
// Load the next interstitial Ad
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
}
To show the ad I used static method.So that I can call this method from another activity.
public static void showInterstitialAd() {
if (mInterstitialAd != null) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}
}
You have to check that the ad is loaded before showing:
if (interstitial!=null && interstitial.isLoaded())
{
interstitial.show();
}

Android java.lang.RuntimeException error

I'm trying to build my Android plugin for Unity. But when I do, I get the follow error log in Logcat:
12-11 14:45:38.745: E/AndroidRuntime(26806): FATAL EXCEPTION: main
12-11 14:45:38.745: E/AndroidRuntime(26806): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.os.Looper.loop(Looper.java:137)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 14:45:38.745: E/AndroidRuntime(26806): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806): at java.lang.reflect.Method.invoke(Method.java:525)
12-11 14:45:38.745: E/AndroidRuntime(26806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 14:45:38.745: E/AndroidRuntime(26806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 14:45:38.745: E/AndroidRuntime(26806): at dalvik.system.NativeStart.main(Native Method)
12-11 14:45:38.745: E/AndroidRuntime(26806): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-1, /vendor/lib, /system/lib]]
12-11 14:45:38.745: E/AndroidRuntime(26806): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 14:45:38.745: E/AndroidRuntime(26806): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 14:45:38.745: E/AndroidRuntime(26806): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 14:45:38.745: E/AndroidRuntime(26806): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 14:45:38.745: E/AndroidRuntime(26806): ... 11 more
I did a search on to why this is happening and I discovered it is because of a problem with my intent. And that I needed to add my activity to my Manifest file. However, this is my manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="7" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.example.marc.CompassActivity"
android:label="#string/app_name">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And from what I can see, my intent is there in the line <activity android:name="com.example.marc.CompassActivity" unless my thinking behind this is entirely wrong?
Just for completions sake, here is my java file:
package com.example.marc;
import com.unity3d.player.UnityPlayerActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.app.Activity;
public class CompassActivity extends UnityPlayerActivity
{
private static final String TAG = "Compass";
private SensorManager mSensorManager;
private Sensor mSensor;
static public float xmag;
static public float ymag;
static public float zmag;
private final SensorEventListener mListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
if (Config.DEBUG) Log.d(TAG,
"sensorChanged (" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + ")");
xmag = event.values[0];
ymag = event.values[1];
zmag = event.values[2];
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
};
#Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
#Override
protected void onResume()
{
if (Config.DEBUG) Log.d(TAG, "onResume");
super.onResume();
mSensorManager.registerListener(mListener, mSensor,
SensorManager.SENSOR_DELAY_GAME);
}
#Override
protected void onStop()
{
if (Config.DEBUG) Log.d(TAG, "onStop");
mSensorManager.unregisterListener(mListener);
super.onStop();
}
public static float getX()
{
return xmag;
}
public static float getY()
{
return ymag;
}
public static float getZ()
{
return zmag;
}
}
Again, as per my thinking, this is what I should be passing to my Manifest file, such, it is called "CompassActivity". Clearly though I must have done something wrong otherwise I wouldn't be getting the errors I am. Could someone please tell me what it is I am doing wrong / missing?
edit
Latest LogCat files:
12-11 16:32:16.957: E/AndroidRuntime(29055): FATAL EXCEPTION: main
12-11 16:32:16.957: E/AndroidRuntime(29055): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.marc/com.example.marc.CompassActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.os.Looper.loop(Looper.java:137)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 16:32:16.957: E/AndroidRuntime(29055): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055): at java.lang.reflect.Method.invoke(Method.java:525)
12-11 16:32:16.957: E/AndroidRuntime(29055): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 16:32:16.957: E/AndroidRuntime(29055): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 16:32:16.957: E/AndroidRuntime(29055): at dalvik.system.NativeStart.main(Native Method)
12-11 16:32:16.957: E/AndroidRuntime(29055): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.marc.CompassActivity" on path: DexPathList[[zip file "/data/app/com.example.marc-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.marc-2, /vendor/lib, /system/lib]]
12-11 16:32:16.957: E/AndroidRuntime(29055): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
12-11 16:32:16.957: E/AndroidRuntime(29055): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-11 16:32:16.957: E/AndroidRuntime(29055): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-11 16:32:16.957: E/AndroidRuntime(29055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-11 16:32:16.957: E/AndroidRuntime(29055): ... 11 more
I think
<activity android:name="com.example.marc.CompassActivity"
SHOULD BE
<activity android:name="CompassActivity"
(you already mentioned "com.example.marc" in package attribute
The value of android:name should be name of activity class only.
Since your manifest file says main activity class is "com.example.marc.CompassActivity", runtime environment looks for it , but it doesn't find it as your main activity class file is named as "CompassAcitvity")
[EDIT]
Main issue is that the runtime is unable to Instantiate the MainActivity because it is not able to find the class "com.example.marc.CompassActivity" in DexPathList
See method "findClass()" line 310-323
If a class is successfully found then a class object is returned other wise null it is returned to BaseDexClassLoader(see line 58) And ClassNotFoundException is thrown in case null is received which is happening in this case.
I think either main activity class is excluded during build process or it has some different name unlike mentioned in manifest.
Your UnityPlayerActivity class is not found actually but Compiler shows as CompassActivity not found.
If you have UnityPlayerActivity in library and that is linked to you app then double check that your Project-->Right Click-->Properties-->Java Build Path--->Order and Export has your project checked.
Following #TNR's answer - Your UnityPlayerActivity class is not found actually but LogCat shows as CompassActivity not found.
What you can try to do is:
Rebuild your project (just for testing) using the original UnityPlayerActivity in your manifest. is it working? (if not then fix this problem first)
Use JD to decompile the JAR where CompassActivity is located. Does it inherit from UnityPlayerActivity? In my case there was a problem with the compiler and it changed the inheritance to NPUnityPlayerActivity which was not found. If this problem occurs you need to recompile your code and make sure the inheritance is correct.
Good Luck!

CalendarView in Dialog

i would like to display a CalendarView in a dialog , at the center of the screen.
Why in the dialog ? Because when i open my CalendarView , it's take all the screen , whereas i would like to display it , in dialog to see the background..
So it's my calendar_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CalendarView
android:id="#+id/calendarView1"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_marginLeft="150dp"/>
</RelativeLayout>
and it's my method who open my calendar
public void showCalendar() {
Dialog dialog = new Dialog(getBaseContext());
// setContentView(R.layout.calendar_main);
cal = (CalendarView) findViewById(R.id.calendarView1);
dialog.setContentView(R.layout.calendar_main);
cal.setOnDateChangeListener(new OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"Selected Date is\n\n"
+dayOfMonth+" : "+month+" : "+year ,
Toast.LENGTH_LONG).show();
}
});
}
Without Dialog , my calendar open correctly with the good size that i defined in xml. but with dialog, my app crash..
It's my error log :
E/AndroidRuntime(27862): FATAL EXCEPTION: main
E/AndroidRuntime(27862): java.lang.NullPointerException
E/AndroidRuntime(27862): at com.pdf.GetViewPDF.showCalendar(GetViewPDF.java:90)
E/AndroidRuntime(27862): at com.pdf.GetViewPDF.onOptionsItemSelected(GetViewPDF.java:113)
E/AndroidRuntime(27862): at android.app.Activity.onMenuItemSelected(Activity.java:2453)
E/AndroidRuntime(27862): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:846)
E/AndroidRuntime(27862): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
E/AndroidRuntime(27862): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:956)
E/AndroidRuntime(27862): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:170)
E/AndroidRuntime(27862): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:85)
E/AndroidRuntime(27862): at android.view.View.performClick(View.java:3117)
E/AndroidRuntime(27862): at android.view.View$PerformClick.run(View.java:11941)
E/AndroidRuntime(27862): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(27862): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(27862): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(27862): at android.app.ActivityThread.main(ActivityThread.java:4123)
E/AndroidRuntime(27862): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27862): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(27862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(27862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(27862): at dalvik.system.NativeStart.main(Native Method)
Otherwise , i call showCalendar from a item buton in my ActionBar
and the NullPointerException appear when i call my listener :
cal.setOnDateChangeListener(new OnDateChangeListener() {...
Try to change this:
cal = (CalendarView) findViewById(R.id.calendarView1);
dialog.setContentView(R.layout.calendar_main);
to this:
dialog.setContentView(R.layout.calendar_main);
cal = (CalendarView) dialog.findViewById(R.id.calendarView1);

Categories