0

我在调试我的应用程序时收到错误消息。

03-21 20:44:33.027: E/AndroidRuntime(4773): FATAL EXCEPTION: main
03-21 20:44:33.027: E/AndroidRuntime(4773): java.lang.RuntimeException: Unable to start  activity ComponentInfo{nt.finger.paint/nt.finger.paint.FingerPaint}: java.lang.NullPointerException
03-21 20:44:33.027: E/AndroidRuntime(4773): Caused by: java.lang.NullPointerException
03-21 20:44:33.027: E/AndroidRuntime(4773):     at nt.finger.paint.FingerPaint.onCreate(FingerPaint.java:67)

我必须做什么?

在这里,您有我的第一个活动的 FingerPaint.java 的完整代码。ImageViewLine 在 R.id 中注册,我只有一个名为 main.xml 的 XML 文件,并且我有所有图像。

public class FingerPaint extends Activity {
Paint mPaint;
private static final String TAG = "FingerPaint";
DrawView drawView;
ImageView imageViewLine;
ImageView imageViewRectangle;
static LinearLayout ll;

private final int CONTEXT_MENU_LINES_ID = 1;
private final int CONTEXT_MENU_SHAPES_ID = 2;
private final int CONTEXT_MENU_COLOR_ID = 3;

private IconContextMenu iconContextMenuLine = null;
private IconContextMenu iconContextMenuShapes = null;
private IconContextMenu iconContextMenuColors = null;

private final int MENU_ITEM_1_ACTION = 1;
private final int MENU_ITEM_2_ACTION = 2;
private final int MENU_ITEM_3_ACTION = 3;
private final int MENU_ITEM_4_ACTION = 4;

private ImageView imageViewText;

private ImageView imageViewColor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initContextMenus();

    drawView = (DrawView)this.findViewById(R.id.DrawView);

    ll = (LinearLayout) this.findViewById(R.id.linearLayout2);


    imageViewLine = (ImageView)this.findViewById(R.id.imageViewLine);
    imageViewLine.setOnClickListener(new OnClickListener() { //there is error

        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Line tool clicked", Toast.LENGTH_SHORT).show();
            //myView.setMode(1); //draw line
            //myView.setOnTouchListener(myView.drawLineListener);
            showDialog(CONTEXT_MENU_LINES_ID,null);
        }
    });

    imageViewRectangle = (ImageView)this.findViewById(R.id.imageViewSquare);
    imageViewRectangle.setOnTouchListener(new OnTouchListener() {

        @SuppressWarnings("deprecation")
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                //Toast.makeText(getApplicationContext(), "Rectangle tool clicked", Toast.LENGTH_SHORT).show();
                //myView.setMode(5); //draw rectangle
                showDialog(CONTEXT_MENU_SHAPES_ID,null);
                return true;
            }
            return false;
        }
    });

    imageViewText = (ImageView)this.findViewById(R.id.imageViewText);
    imageViewText.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            drawView.setMode(6);//text input
        }
    });

    imageViewColor = (ImageView)this.findViewById(R.id.imageViewColor);
    imageViewColor.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Select color", Toast.LENGTH_SHORT).show();
            //myView.setMode(5); //draw rectangle
            showDialog(CONTEXT_MENU_COLOR_ID,null);


        }
    });
}

protected Dialog onCreateDialog(int id) {
    if (id == CONTEXT_MENU_LINES_ID) {
        return iconContextMenuLine.createMenu("Lines");
    }
    else if(id == CONTEXT_MENU_SHAPES_ID){
        return iconContextMenuShapes.createMenu("Shapes");
    }
    else if(id == CONTEXT_MENU_COLOR_ID){
        return iconContextMenuColors.createMenu("Choose color");
    }
    return super.onCreateDialog(id);
}

    public  void initContextMenus(){
        Resources res = getResources();
        //init icon context menu
        iconContextMenuLine = new IconContextMenu(this, CONTEXT_MENU_LINES_ID);
        iconContextMenuLine.addItem(res, "", R.drawable.line, MENU_ITEM_2_ACTION);
        iconContextMenuLine.addItem(res, "", R.drawable.linie_taiata, MENU_ITEM_4_ACTION);

        iconContextMenuLine.setOnClickListener(new IconContextMenu.IconContextMenuOnClickListener() {
            public void onClick(int menuId) {
                switch(menuId) {

                case MENU_ITEM_2_ACTION:
                    //Toast.makeText(getApplicationContext(), "You've clicked on menu item 2", Toast.LENGTH_SHORT).show();
                    drawView.normal_line();
                    drawView.setMode(1); //draw line
                    break;
                case MENU_ITEM_4_ACTION:
                    //Toast.makeText(getApplicationContext(), "You've clicked on menu item 2", Toast.LENGTH_SHORT).show();
                    drawView.dash_line();
                    drawView.setMode(1);//draw  dashed line
                    break;

                }
            }
        });

        iconContextMenuShapes = new IconContextMenu(this, CONTEXT_MENU_SHAPES_ID);
        iconContextMenuShapes.addItem(res, "", R.drawable.cerc,MENU_ITEM_1_ACTION);
        iconContextMenuShapes.addItem(res, "",R.drawable.square, MENU_ITEM_2_ACTION);

        iconContextMenuShapes.setOnClickListener(new IconContextMenu.IconContextMenuOnClickListener() {
            public void onClick(int menuId) {

                switch(menuId) {

                case MENU_ITEM_1_ACTION:
                    //Toast.makeText(getApplicationContext(), "You've clicked on menu item 2", Toast.LENGTH_SHORT).show();
                    drawView.setMode(4); //draw circle
                    break;
                case MENU_ITEM_2_ACTION:
                    //Toast.makeText(getApplicationContext(), "You've clicked on menu item 2", Toast.LENGTH_SHORT).show();
                    drawView.setMode(5); //draw  rectangle
                    break;

                }
            }
        });

        iconContextMenuColors = new IconContextMenu(this, CONTEXT_MENU_SHAPES_ID);
        iconContextMenuColors.addItem(res, "", R.drawable.red,MENU_ITEM_1_ACTION);
        iconContextMenuColors.addItem(res, "",R.drawable.green, MENU_ITEM_2_ACTION);
        iconContextMenuColors.addItem(res, "",R.drawable.blue, MENU_ITEM_3_ACTION);

        iconContextMenuColors.setOnClickListener(new IconContextMenu.IconContextMenuOnClickListener() {
            public void onClick(int menuId) {

                switch(menuId) {

                case MENU_ITEM_1_ACTION:
                    Toast.makeText(getApplicationContext(), "Red", Toast.LENGTH_SHORT).show();
                    drawView.changeColour(Color.RED);

                    break;
                case MENU_ITEM_2_ACTION:
                    Toast.makeText(getApplicationContext(), "Green", Toast.LENGTH_SHORT).show();
                    drawView.changeColour(Color.GREEN);
                    break;

                case MENU_ITEM_3_ACTION:
                    Toast.makeText(getApplicationContext(), "Blue", Toast.LENGTH_SHORT).show();
                    drawView.changeColour(Color.BLUE);

                    break;

                }
            }
        });


    // Set full screen view


    // lock screen orientation (stops screen clearing when rotating phone)
    setRequestedOrientation(getResources().getConfiguration().orientation);

    drawView = new DrawView(this, null);
    setContentView(drawView);
    drawView.setBackgroundColor(Color.WHITE);
    drawView.requestFocus();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.paint_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.clear_id : {
        drawView.clearPoints();
        return true;
    }
    case R.id.p_white_id : {
        drawView.changeColour(0);
        return true;
    }
    case R.id.w_small : {
        drawView.changeWidth(5);
        return true;
    }
    case R.id.w_medium : {
        drawView.changeWidth(10);
        return true;
    }
    case R.id.w_large : {
        drawView.changeWidth(15);
        return true;
    }
    case R.id.text : {
        drawView.setInputDialog();
        return true;
    }
    default : {
        return true;
    }
     case R.id.tools:
        ll.setVisibility(LinearLayout.VISIBLE);
        return true;
     case R.id.b_custom_id:
        setCustomBackground(drawView);
        return true; 
    }
}

void setCustomBackground(DrawView v) {
    Intent fileChooserIntent = new Intent();
    fileChooserIntent.addCategory(Intent.CATEGORY_OPENABLE);
    fileChooserIntent.setType("image/*");
    fileChooserIntent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(fileChooserIntent, "Select Picture"), 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if statement prevents force close error when picture isn't selected
    if (resultCode == RESULT_OK)
    {
        Uri resultUri = data.getData();
        //String resultString = data.getData().toString();

        String drawString = resultUri.getPath();
        String galleryString = getGalleryPath(resultUri);

        // if Gallery app was used
        if (galleryString != null)
        {
            Log.d(TAG, galleryString);
            drawString = galleryString;
        }
        // else another file manager was used
        else
        {
            Log.d(TAG, drawString);
            //File Manager: "content://org.openintents.cmfilemanager/mimetype//mnt/sdcard/DCIM/Camera/IMG_20110909_210412.jpg"
            //ASTRO:        "file:///mnt/sdcard/DCIM/Camera/IMG_20110924_133324.jpg"
            if (drawString.contains("//"))
            {
                drawString = drawString.substring(drawString.lastIndexOf("//"));
            }
        }

        // set the background to the selected picture
        if (drawString.length() > 0)
        {
            Drawable drawBackground = Drawable.createFromPath(drawString);
            drawView.setBackgroundDrawable(drawBackground);
        }

    }
}

// used when trying to get an image path from the URI returned by the Gallery app
public String getGalleryPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);

    if (cursor != null)
    {
        int column_index =     cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


    return null;
}

}

main.xml 在这里你有我唯一的一个 xml 文件。希望对我有用!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
    <nt.finger.paint.DrawView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="3" 
        android:id = "@+id/DrawView"/>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_weight="0.08"
        android:background="@drawable/gray_bar"
        android:orientation="horizontal" >

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewColor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/culoare" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewBar0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewSquare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/square" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewText"
            android:layout_width="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/a" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewLine"
            android:layout_width="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/line" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewArrow"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_gravity="center"
            android:src="@drawable/arrow" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewSquiggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/squiggle" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewEraser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/erase" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewUndo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/undo" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewRedo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/redo" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewHand"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/hand" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewCrop"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/crop" />

         <ImageView
             android:contentDescription="@string/description"
            android:id="@+id/imageViewBar10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/bar" />

        <ImageView
            android:contentDescription="@string/description"
            android:id="@+id/imageViewDottedSquare"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/patrat_punctat" />

    </LinearLayout>

</LinearLayout>
4

2 回答 2

1

尝试this.在您的findViewById方法之前删除。也许您目前不在您的活动环境中。

于 2013-03-21T19:11:55.387 回答
0

在实际使用图像之前,请确保您R.id.imageViewLine从正确的布局中引用资源。

有时我们会遇到我们认为在当前布局中可用的资源实例,但该资源实际上是与当前活动布局不同的布局的一部分。这将导致NullPointerException我们去设置它的第一个属性。

编辑: 在代码中,您似乎确实设置了另一个当前布局;现在已经引入了附加代码。这可能是这里的问题。这可以从 -> 的调用堆栈跟踪中找到 onCreate();- initContextMenus();>setContentView(drawView); 这可能会干扰 ImageView 的检索。

于 2013-03-21T18:57:48.243 回答