从另一个活动更改画布颜色

时间:2016-03-12 21:03:04

标签: java android

您好我喜欢从MainActivity更改画布颜色。尝试了几种方法,但由于缺乏知识无法做到这一点。如果有人知道该怎么做将是一个很大的帮助。这是我的代码的快照:

当前状态:当我选择橙色屏幕时,将垂直旋转到水平并崩溃 pics

activity_main.xml中

<xscourse.some.GameView
    android:id="@+id/pntr"
    android:tag="Paint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    /

MainActivity.java

public class MainActivity extends AppCompatActivity {
public Canvas canvas;
Paint paint  = new Paint();
GameView gameView;
private boolean mDrawingEnabled = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_orange) {
              gameView.mBackgroundPaint.setColor(Color.parseColor("#FFCC80"));

   gameView.draw(canvas);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

GameView.java

public class GameView extends View {
final Paint mBackgroundPaint;
final BoardGeometry mBoardGeometry;
public GameView(Context context, AttributeSet atrs) {
    super(context, atrs);

    mBoardGeometry = new BoardGeometry(50, 100, 260, 310, 70, 70);
    mBackgroundPaint = new Paint();
    mBackgroundPaint.setColor(Color.GREEN);
 }
 @Override
public void draw(Canvas canvas) {
    if ( mDrawingEnabled ) {
        final int width = canvas.getWidth();
        final int height = canvas.getHeight();
        // 1. draw background rectangle that covers the entire
        // canvas
        canvas.drawRect(0, 0, width, height, mBackgroundPaint);

        invalidate();
    }
}

我需要更改onOptionsItemSelected(MenuItem项)的背景。怎么做?

Crash Stacktrace

03-12 16:25:57.131 6763-6763/xscourse.houndsandhare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: xscourse.houndsandhare, PID: 6763
                                                                      java.lang.NullPointerException: Attempt to read from field 'android.graphics.Paint xscourse.houndsandhare.GameView.mBackgroundPaint' on a null object reference
                                                                          at xscourse.houndsandhare.MainActivity.onOptionsItemSelected(MainActivity.java:51)
                                                                          at android.app.Activity.onMenuItemSelected(Activity.java:2885)
                                                                          at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
                                                                          at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:185)
                                                                          at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:655)
                                                                          at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                                                                          at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                                                                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
                                                                          at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
                                                                          at android.widget.AdapterView.performItemClick(AdapterView.java:305)
                                                                          at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
                                                                          at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
                                                                          at android.widget.AbsListView$3.run(AbsListView.java:3860)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          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)
03-12 16:25:57.132 728-1058/system_process W/ActivityManager:   Force finishing activity 1 xscourse.houndsandhare/.MainActivity
0

0 个答案:

没有答案