空指针异常。无法弄清楚如何修复

时间:2012-03-08 23:37:23

标签: android nullpointerexception android-logcat

好的我有一个简单的主要活动,只是为用户提供了一个点击按钮,我稍后会添加更多按钮。 当我去编译时,它会编译,但它会立即在我的设备上关闭。

继承我的活动代码后跟我的logcat输出。

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);
  }





}

继承我的LogCat输出。

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

继承人基础活动

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();

 }
}

继承人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>

2 个答案:

答案 0 :(得分:2)

编辑:这与main.xml或Button无关。以下行抛出空指针:

tracker.trackPageView("/ApplicationHomeScreen");

查看BaseActivity没有为tracker指定访问修饰符,因此它采用默认访问修饰符,显然不允许访问继承类,即使它们是在同一个包中。看看你的代码,这两个类似乎在同一个包中。无论如何,明确地将tracker定义为protected将允许从继承类访问它,而不管包。

我还是有点难过。就我而言,它不应该编译。不知何故,跟踪器变量是可访问的,但它的实例化并不是......任何潜藏在某些光线下的Java专家?

答案 1 :(得分:1)

无论tracker是什么,它都没有被初始化。我看到你有一个扩展的基类,所以确保在超类中正确设置了tracker。这意味着要么在构造函数中,要么在super.onCreate()