Null poiner调用Toolbar.getTitle()

时间:2016-04-27 12:51:08

标签: java android sprite

测试精灵代码我收到以下错误:

FATAL EXCEPTION: main
                                                            Process: marti.sprite1app, PID: 2149
                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{marti.sprite1app/marti.sprite1app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:135)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
                                                                at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
                                                                at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
                                                                at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
                                                                at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
                                                                at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
                                                                at marti.sprite1app.MainActivity.onCreate(MainActivity.java:19)
                                                                at android.app.Activity.performCreate(Activity.java:5990)
                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:135) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 

我试图显示一个精灵并在屏幕上显示它,更改了该课程的视图&#34; vistaPrincipalJuego&#34;。 对不起,英语不是很好。

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(new VistaPrincipalJuego(this));
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

线程子进程:

public class SubprocesoPrincipal extends Thread {

private boolean finJuego;
private SurfaceHolder surfaceHolder;
private VistaPrincipalJuego vistaJuego;

public SubprocesoPrincipal (SurfaceHolder surfaceHolder, VistaPrincipalJuego vista) {

    super();

    this.surfaceHolder = surfaceHolder;
    this.vistaJuego = vista;

}

public void setFinJuego(boolean fj) {

    this.finJuego = fj;

}

public void run() {

    Canvas lienzo = null;

    while (!finJuego) {

        try {

            lienzo = this.surfaceHolder.lockCanvas();

            synchronized (surfaceHolder) {

                this.vistaJuego.draw(lienzo);
            }

        } finally {

            if (lienzo != null) {

                surfaceHolder.unlockCanvasAndPost(lienzo);

            }
        }

    }

}

}

类扩展了SurfaceView:

public class VistaPrincipalJuego extends SurfaceView implements SurfaceHolder.Callback {

private SubprocesoPrincipal buclePrincipal;

public VistaPrincipalJuego(Context context) {

    super(context);

    buclePrincipal = new SubprocesoPrincipal(getHolder(), this);

    getHolder().addCallback(this);

    setFocusable(true);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    this.buclePrincipal.setFinJuego(false);
    this.buclePrincipal.start();

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    boolean repite = true;

    while (repite) {

        try {

            buclePrincipal.join();
            repite = false;

        } catch (InterruptedException e) {

        }

    }


}

public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        if (event.getY() > getHeight() - 50) {

            buclePrincipal.setFinJuego(true);
            ((Activity) getContext()).finish();

        }

    }

    return super.onTouchEvent(event);

}

protected void onDraw(Canvas lienzo) {

    lienzo.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.mario), 10, 10, null);

}

}

1 个答案:

答案 0 :(得分:0)

简单的决定 - 尝试删除所有这些示例代码:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });