-1

我正在写一个溺水的应用程序。目前我有下一个问题,当我单击“R.layout.activity_colors_prefs”中的“redBtn”时出现下一个错误。

   08-26 03:27:31.576: E/AndroidRuntime(706): FATAL EXCEPTION: main
    08-26 03:27:31.576: E/AndroidRuntime(706): java.lang.NullPointerException
    08-26 03:27:31.576: E/AndroidRuntime(706):  at com.example.drawview.ChooseColor$1.onClick(ChooseColor.java:43)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.view.View.performClick(View.java:4084)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.view.View$PerformClick.run(View.java:16966)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.os.Handler.handleCallback(Handler.java:615)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.os.Handler.dispatchMessage(Handler.java:92)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.os.Looper.loop(Looper.java:137)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at java.lang.reflect.Method.invoke(Method.java:511)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-26 03:27:31.576: E/AndroidRuntime(706):  at dalvik.system.NativeStart.main(Native Method)

根据调试模式,问题出在此命令中:

  • view_a.setdrawColor(Color.RED);

在下一节课

public class ChooseColor extends Activity  {
    //View
    private DrawingView view_a;

    //Button
    Button redBtn;
    public int colorPref= Color.BLACK;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colors_prefs);
        redBtn = (Button) findViewById(R.id.redColr);
        redBtn.setOnClickListener(myButtonListener);
        view_a=(DrawingView) findViewById(R.id.drawingView);
    }

    public int getColorPref() {
        return colorPref;
    }


    private OnClickListener myButtonListener = new OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.redColr:
                Toast.makeText(getBaseContext(), "RED", Toast.LENGTH_SHORT).show();
                view_a.setdrawColor(Color.RED);     
                break;
            default:
                break;
            }

        }
    };

The method "setdrawColor" is the next:

public void setdrawColor(int color) 
{ 
   //create a copy of bitmap before changing the paint color 
     bitmap = Bitmap.createBitmap(bitmap); 
     canvas.setBitmap(bitmap); 
     paint.setColor(color); 
}

“setdrawColor”位于其他活动中,称为“DrawingView”的活动。“DrawingView”负责绘制并扩展 View 而不会扩展 Activity。

你能告诉我有什么问题吗?我究竟做错了什么?

4

1 回答 1

2

在调用方法并报告结果之前执行此操作

if (view_a != null) {
  view_a.setdrawColor(Color.RED);
} else {
  Log.e("ChooseColor","view_a is null!");
}

我在想 view_a 永远不会正确设置。你能证实这一点吗?

于 2012-08-26T05:09:56.463 回答