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();
}
Related
I am attempting to integrate Mixpanel into a simple application using the instructions here. However I am getting an error that is causing my App to crash.
I have no syntax errors in my code (see below).
Android Activity:
package com.example.testmixpanel;
import org.json.JSONException;
import org.json.JSONObject;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxx"; //intentionally not shown
// Initialize the library with your
// Mixpanel project token, MIXPANEL_TOKEN, and a reference
// to your application context.
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(this, MIXPANEL_TOKEN);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush();
super.onDestroy();
}
}
However, when I try to run the application I get this error:
0-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
What could be the cause of this issue?
Full stack trace (for reference):
10-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.access$700(ActivityThread.java:159)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.os.Looper.loop(Looper.java:176)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.main(ActivityThread.java:5419)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-09 16:36:31.575: E/AndroidRuntime(13192): at dalvik.system.NativeStart.main(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): Caused by: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.mixpanel.android.mpmetrics.MixpanelAPI.getInstance(MixpanelAPI.java:175)
10-09 16:36:31.575: E/AndroidRuntime(13192): at com.example.testmixpanel.MainActivity.<init>(MainActivity.java:22)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstanceImpl(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): at java.lang.Class.newInstance(Class.java:1130)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
10-09 16:36:31.575: E/AndroidRuntime(13192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
10-09 16:36:31.575: E/AndroidRuntime(13192): ... 11 more
EDIT:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MixpanelAPI mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
#Override
protected void onDestroy() {
mixpanel.flush(); //how to call this as mixpanel is not a class variable?
super.onDestroy();
}
}
If you want to use the mixpanel on onDestroy you should instantiate it like this:
public class MainActivity extends Activity {
public static final String MIXPANEL_TOKEN = "xxxxx";
MixpanelAPI mixpanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mixpanel =
MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);
setContentView(R.layout.activity_main);
JSONObject props = new JSONObject();
mixpanel.track("Plan Selected", props);
}
(...)
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!
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);
I'm new to Android development and cant work out the issue here. I create a blank class called video with two properties name and url:
public class Video {
public String _name;
public String _Url;
public Video(String name, String Url)
{
_name = name;
_Url = Url;
}
public String getName()
{
return _name;
}
public String getUrl()
{
return _Url;
}
}
I then have a generic list for adding videos to in an activity calls VideosListActivity, the error is being thrown when i add the video to the list:
public List<Video> ListResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Update view
setContentView(R.layout.videos);
setTitle("VIDEOS");
//--videos created here
Video NewVideo = new Video("video one","http://www.youtube.com/watch?v=cxLG2wtE7TM");
ListResult.add(NewVideo);
Log.v("VideoList", "Opened list");
this error is what is thrown:
02-06 13:10:05.660: E/AndroidRuntime(23432): FATAL EXCEPTION: main
02-06 13:10:05.660: E/AndroidRuntime(23432): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.mosquitodigital.panic/uk.co.mosquitodigital.panic.VideoListActivity}: java.lang.NullPointerException
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.access$600(ActivityThread.java:139)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.os.Looper.loop(Looper.java:156)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.main(ActivityThread.java:5045)
02-06 13:10:05.660: E/AndroidRuntime(23432): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 13:10:05.660: E/AndroidRuntime(23432): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 13:10:05.660: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-06 13:10:05.660: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-06 13:10:05.660: E/AndroidRuntime(23432): at dalvik.system.NativeStart.main(Native Method)
02-06 13:10:05.660: E/AndroidRuntime(23432): Caused by: java.lang.NullPointerException
02-06 13:10:05.660: E/AndroidRuntime(23432): at uk.co.mosquitodigital.panic.VideoListActivity.onCreate(VideoListActivity.java:31)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.Activity.performCreate(Activity.java:4543)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
02-06 13:10:05.660: E/AndroidRuntime(23432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
02-06 13:10:05.660: E/AndroidRuntime(23432): ... 11 more
you will need to initialize ListResult List before adding elements to it as :
public List<Video> ListResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Update view
setContentView(R.layout.videos);
ListResult= new ArrayList<Video>(); //<< initialize List here
when testing my application in the emulator(I'm using Eclipse) it shows me "The application Counter(com.ian.counter) has stopped unexpectedly. Please try again." when running it. I've searched and searched for an answer but I've found none. Anyone know the problem?
Code:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1 = (TextView) this.findViewById(R.id.textView1);
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Logcat:
12-28 16:59:20.615: D/AndroidRuntime(353): Shutting down VM
12-28 16:59:20.686: W/dalvikvm(353): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-28 16:59:20.756: E/AndroidRuntime(353): FATAL EXCEPTION: main
12-28 16:59:20.756: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ian.counter/com.ian.counter.CounterActivity}: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:507)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 16:59:20.756: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): Caused by: java.lang.NullPointerException
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Activity.findViewById(Activity.java:1647)
12-28 16:59:20.756: E/AndroidRuntime(353): at com.ian.counter.CounterActivity.<init>(CounterActivity.java:10)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:59:20.756: E/AndroidRuntime(353): at java.lang.Class.newInstance(Class.java:1409)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:59:20.756: E/AndroidRuntime(353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
Any help is appreciated.
You're trying to set the textView1 variable before the layout has been set. You need to define it as a variable and then assign it in onCreate() after you've called setContentView(). Like this:
package com.ian.counter;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import android.widget.Toast;
public class CounterActivity extends Activity {
/** Called when the activity is first created. */
TextView textView1;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView) this.findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}
}
Just copy and paste the above, that'll work just fine.
You should put this line
textView1 = (TextView) this.findViewById(R.id.textView1);
after
setContentView(R.layout.main);
and you just declare TextView in the begining
TextView textView1;
dont try to use findViewById(R.id.textView1); while declaraing instance variable as view does not exist at that point of time.
This is how your code should look like:
TextView textView1 = null;
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
}
public void Count(){
count ++;
textView1.setText(Integer.toString(count));
}