0

我正在尝试Activity使用Intent. 我已经在另一个应用程序中成功完成了这项工作,但这种方法在这种情况下似乎不起作用?

这是我的代码...

 public void startLevelEnd(Context context){
     Intent myIntent = new Intent(FirstActivity.this, LevelEndPanel.class);
     FirstActivity.this.startActivity(myIntent);
 }

从我的AndroidManifest.xml档案...

<activity android:label="@string/app_name" android:name=".LevelEndPanel"></activity>

startLevelEnd()使用...调用例程(来自不同的类)

FirstActivity.startLevelEnd(this);

NullPointerException在“Intent myIntent ...”行(来自上面的代码片段)上收到错误:

04-16 09:46:39.361: E/AndroidRuntime(475):  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)

完整日志:

04-16 14:32:32.870: E/AndroidRuntime(336): FATAL EXCEPTION: main
04-16 14:32:32.870: E/AndroidRuntime(336): java.lang.NullPointerException
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.content.ComponentName.<init>(ComponentName.java:75)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.content.Intent.<init>(Intent.java:2702)
04-16 14:32:32.870: E/AndroidRuntime(336):  at happyworx.nl.App.FirstActivity.startLevelEnd(FirstActivity.java:32)
04-16 14:32:32.870: E/AndroidRuntime(336):  at happyworx.nl.App.model.components.GameManager.LevelEnd(GameManager.java:89)
04-16 14:32:32.870: E/AndroidRuntime(336):  at happyworx.nl.App.model.components.GameManager$1.run(GameManager.java:60)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.os.Handler.handleCallback(Handler.java:587)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
04-16 14:32:32.870: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 14:32:32.870: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:32:32.870: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 14:32:32.870: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 14:32:32.870: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 14:32:32.870: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)

对于@Sergi,包含该方法的完整类:

package happyworx.nl.Appelz.model.components;


import happyworx.nl.Appelz.AppelzActivity;
import happyworx.nl.Appelz.LevelEndPanel;
import happyworx.nl.Appelz.MainGamePanel;
import happyworx.nl.Appelz.MainThread;
import happyworx.nl.Appelz.R;
import happyworx.nl.Appelz.model.apple;
import happyworx.nl.Appelz.model.glyphs;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.Log;


public class GameManager extends Activity {

    //Declare Vars
    private static final String TAG = "GameManager";
    public static MainGamePanel panel;
    private static glyphs glyphsCount;
    public static GameManager GameManager = new GameManager();

    public static AppelzActivity AppelzActivity = new AppelzActivity();

    private static Canvas canvas;
    final static int delayMillis = 1000;
    public static boolean count = false;
    public static int i = 6;
    public final static Handler handler = new Handler();


    public final static Runnable runnable = new Runnable()  {

        @Override
        public void run()
        {

            if (SlotManager.alSlottedFruitList.size() > 0){
            //TODO Create Full screen countdown timer
                Log.d("", "Countdown Timer in GameManager " + i);

                i--;
                if (count && i > -1) // stop timer when count is false
                {
                    handler.postDelayed(runnable, 1000);
                } else {
                    if (i == -1){
                        Log.d("", "RoundUp the Level");

                        GameManager.LevelEnd();
                    }
                    // reset Countdown (if basket outside checkout)
                    i = 6;
                }
            } else {
                // reset Countdown (if slot has no fruits)
                i = 6;
            }
        }
    };


    public void LevelEnd(){
        // First, count the Fruits in the slot
        SlotManager.countSlotInv();
        List<Integer> list = new ArrayList<Integer>();


        // Fill itemIds with the types of fruits
        for (apple a : SlotManager.alSlottedFruitList){
            Integer itemId = Integer.valueOf(a.FruitType);
            list.add(itemId);

            Set<Integer> unique = new HashSet<Integer>(list);
        }
            for (int key = 0; key < 3 ; key++) {
                System.out.println(key + " : " + Collections.frequency(list, key));     
            }
            startLevelEnd();
    }

    public void startLevelEnd(){
         Intent myIntent = new Intent(this, LevelEndPanel.class);
         this.startActivity(myIntent);
     }

    public GameManager() 
    {
    }

LevelEndPanel 类:

package happyworx.nl.Appelz;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class LevelEndPanel extends Activity implements OnClickListener {

 private static final String TAG = LevelEndPanel.class.getSimpleName();
 private Button btnNewMission;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level);

        btnNewMission = (Button) findViewById(R.id.btnNewMission);
        btnNewMission.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(), AppelzActivity.class);
                startActivity(i);
            }});


 }


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

和主线程:

package happyworx.nl.Appelz;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

 private static final String TAG = MainThread.class.getSimpleName();

 private SurfaceHolder surfaceHolder;
 private MainGamePanel gamePanel;
 public static boolean running;
 public static Canvas canvas;

 public void setRunning(boolean running) {
 MainThread.running = running;
 }

 public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
  super();
  this.surfaceHolder = surfaceHolder;
  this.gamePanel = gamePanel;

 }

 @Override
 public void run() {
     // Canvas canvas;
      Log.d(TAG, "Starting game loop");
      while (running) {
      MainThread.canvas = null;
       // try locking the canvas for exclusive pixel editing on the surface
       try {
           MainThread.canvas = this.surfaceHolder.lockCanvas();
        synchronized (surfaceHolder) {
         // update game state
         this.gamePanel.update();
         // render state to the screen
         // draws the canvas on the panel
         this.gamePanel.onDraw(MainThread.canvas);
        }
       } finally {
        // in case of an exception the surface is not left in
        // an inconsistent state
        if (MainThread.canvas != null) {
         surfaceHolder.unlockCanvasAndPost(MainThread.canvas);
        }
       } // end finally
      }
     }
}

还有篮子类,它在 GameManager 中启动 CountDown 计时器(也许这是我的错误?!?)

package happyworx.nl.Appelz.model;

import happyworx.nl.Appelz.MainGamePanel;
import happyworx.nl.Appelz.model.components.GameManager;
import happyworx.nl.Appelz.model.components.SlotManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

public class basket {

 private Bitmap bitmap; // the actual bitmap
 private int x;   // the X coordinate
 private int y;   // the Y coordinate'
 public static int width;
 public static int height;
 public float X = 100;
 public float Y = 100;

 private boolean touched; // if droid is touched/picked up

 public basket(Bitmap bitmap, int x, int y) {
  this.bitmap = bitmap;

  width = bitmap.getWidth(); 
  height = bitmap.getHeight(); 
 }

 public Bitmap getBitmap() {
  return bitmap;
 }
 public void setBitmap(Bitmap bitmap) {
  this.bitmap = bitmap;
 }

 public int getWidth(){
      return bitmap.getWidth();
     }

     public int getHeight(){
      return bitmap.getHeight();
     }

 public int getX() {
  return x;
 }



 public void setX(float x) {

    // if basket is further than slotmargin 
     if (x + width/2 > SlotManager.SlotmarginX) {
        // if basket is NOT in CheckOut area    
         if (this.Y < MainGamePanel.height - 80 - height/2){
            x = SlotManager.SlotmarginX - width/2;

        } else {
            // if basket IS in CheckOut area
            x = MainGamePanel.width - width/2;

            // Start the countdown timer if not already running
            if (!GameManager.count){
                GameManager.count=true;
                GameManager.handler.postDelayed(GameManager.runnable, 0);                               
            }

        }
    } else {

        // Basket is NOT in CheckOut area; stop countdown timer
        GameManager.count=false;
    }

     this.X = x;
 }


 public int getY() {
  return y;
 }
 public void setY(int y) {
        if (y + height/2 > MainGamePanel.height - 80){
            y = MainGamePanel.height - 80 - height/2;
        }


  this.Y = y;
 }


 public boolean isTouched() {
  return touched;
 }

 public void setTouched(boolean touched) {
  this.touched = touched;
 }

 public void draw(Canvas canvas) {
      canvas.drawBitmap(bitmap, X - width/2, Y - height/2, null);
 }

 public void handleActionDown(int eventX, int eventY) {
  if (eventX >= (X - width/ 2) && (eventX <= (X + width/2))) {
   if (eventY >= (Y - height/ 2) && (eventY <= (Y + height/ 2))) {
    // basket touched
    setTouched(true);
   } else {
    setTouched(false);
   }
  } else {
   setTouched(false);
  }

 }
}
4

3 回答 3

7

我敢打赌,您正在尝试从未完全初始化的活动开始活动。

确保在开始另一个活动之前调用了 FirstActivity.onCreate。

于 2012-04-16T15:04:55.993 回答
5

该错误看起来像提供给 Intent 构造函数的上下文为空。

代码应该更像:

public void startLevelEnd(Context context){
     Intent myIntent = new Intent(context, LevelEndPanel.class);
     context.startActivity(myIntent);
 }

确保您startLevelEnd使用此调用从活动中调用

startLevelEnd(this)

如果startLevelEnd位于调用它的同一个 Activity 类中,请简化它:

public void startLevelEnd(){
     Intent myIntent = new Intent(this, LevelEndPanel.class);
     startActivity(myIntent);
 }
于 2012-04-18T07:37:33.460 回答
1

我相当确定您必须为 Intent 中的第一个参数使用上下文,并且由于您在 startLevelEnd 方法中有一个上下文作为参数,您可以尝试

Intent myIntent = new Intent (context, LevelEndPanel.class);
context.startActivity(myIntent);

我认为您收到 NullPointerException 是因为您的班级看不到 FirstActivity。但这取决于 startLevelEnd 方法的位置。既然你不说它在哪里,那就试试吧!

于 2012-04-16T10:33:48.310 回答