Ok I have a simple main activity that just presents the user with a button to click on and I will add more buttons later.
When I go to compile it compiles but it force closes immediately on my device.
Heres my Activity code followed by my logcat output.
package com.michaelpeerman.probability;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.michaelpeerman.probability.R;
public class MainActivity extends BaseActivity
{
View.OnClickListener buttonhandler = new View.OnClickListener()
{
public void onClick(View v)
{
Intent localIntent;
switch (v.getId())
{
case R.id.cointoss:
localIntent = new Intent(MainActivity.this, CoinActivity.class);
MainActivity.this.startActivity(localIntent);
break;
}
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
tracker.trackPageView("/ApplicationHomeScreen");
setContentView(R.layout.main);
Button localButton1 = (Button)findViewById(R.id.cointoss);
//Button localButton2 = (Button)findViewById(R.id.doctor10);
//Button localButton3 = (Button)findViewById(R.id.doctor11);
localButton1.setOnClickListener(this.buttonhandler);
//localButton2.setOnClickListener(this.buttonhandler);
//localButton3.setOnClickListener(this.buttonhandler);
}
}
Heres My LogCat Output.
03-08 18:29:12.664: E/AndroidRuntime(6467): FATAL EXCEPTION: main
03-08 18:29:12.664: E/AndroidRuntime(6467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michaelpeerman.probability/com.michaelpeerman.probability.MainActivity}: java.lang.NullPointerException
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.os.Looper.loop(Looper.java:137)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-08 18:29:12.664: E/AndroidRuntime(6467): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 18:29:12.664: E/AndroidRuntime(6467): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 18:29:12.664: E/AndroidRuntime(6467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-08 18:29:12.664: E/AndroidRuntime(6467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-08 18:29:12.664: E/AndroidRuntime(6467): at dalvik.system.NativeStart.main(Native Method)
03-08 18:29:12.664: E/AndroidRuntime(6467): Caused by: java.lang.NullPointerException
03-08 18:29:12.664: E/AndroidRuntime(6467): at com.michaelpeerman.probability.MainActivity.onCreate(MainActivity.java:36)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.Activity.performCreate(Activity.java:4465)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-08 18:29:12.664: E/AndroidRuntime(6467): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-08 18:29:12.664: E/AndroidRuntime(6467): ... 11 more
Heres base Activity
package com.michaelpeerman.probability;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class BaseActivity extends Activity {
private String donate_link = "market://details?id=com.michaelpeerman.donate";
private String rate_link = "market://details?id=com.michaelpeerman.probability";
private String more_apps = "market://search?q=pub:Michael Peerman";
private String sharetext = "Check out this amazing Probability app." + "\n"
+ "\n" + "http://goo.gl/yU3jy";
GoogleAnalyticsTracker tracker;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("REMOVED", 60, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.donate:
tracker.trackEvent("Menu_Item", "donate", "clicked", 0);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(donate_link)));
break;
case R.id.rate:
tracker.trackEvent("Menu_Item", "rate", "clicked", 0);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(rate_link)));
break;
case R.id.moreapps:
tracker.trackEvent("Menu_Item", "more_apps", "clicked", 0);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(more_apps)));
break;
case R.id.share:
tracker.trackEvent("Menu_Item", "share", "clicked", 0);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent
.putExtra(android.content.Intent.EXTRA_TEXT, sharetext);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
break;
}
return true;
}
@Override
public void onPause() {
super.onPause();
tracker.dispatch();
}
@Override
public void onDestroy() {
super.onDestroy();
tracker.dispatch();
tracker.stopSession();
}
}
And heres Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow
android:background="@drawable/coin_toss_banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TableRow>
<Button android:id="@+id/cointoss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Coin Toss" />
</LinearLayout>
EDIT: This has nothing to do with main.xml or the Button. The following line is throwing the null pointer:
Looking at
BaseActivitythere is no access modifier specified fortracker, so it takes on the default access modifier, which apparently does not allow access to inheriting classes, even if they’re in the same package. Looking at your code, the two classes do seem to be in the same package. Regardless, explicitly definingtrackerasprotectedwill allow it to be accessed from inheriting classes, regardless of package.I’m still a little stumped by this. As far as I’m concerned it shouldn’t have compiled. Somehow the tracker variable is accessible, but the instantiation of it is not…any Java experts lurking to shed some light?