NullPointerException错误的原因是什么

时间:2012-08-26 05:05:07

标签: android nullpointerexception

我正在写一个drowing应用程序。目前我有下一个问题,当我点击“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。

请问edvice有什么问题?我做错了什么?

1 个答案:

答案 0 :(得分:2)

在调用方法并报告结果

之前执行此操作
if (view_a != null) {
  view_a.setdrawColor(Color.RED);
} else {
  Log.e("ChooseColor","view_a is null!");
}

我认为view_a从未正确设置。你能证实一下吗?