2013-03-21 64 views
0

我在调试我的应用程序时收到错误。java.lang.NullPointerException

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

没有ImageView调用'imageViewLine' – 2013-03-21 18:55:01

+0

android:id =“@ + id/imageViewLine”this? – 2013-03-21 18:58:08

+0

好吧。你确定它在你正在加载的同一个XML文件中吗? – 2013-03-21 19:09:42

回答

0

请务必在实际使用图像之前从正确的布局引用资源R.id.imageViewLine

偶尔我们会遇到一个资源实例,我们认为这个实例在我们当前的布局中可用,但是实际上资源实际上是与当前布局不同的布局的一部分。一旦我们设置其第一个属性,会导致NullPointerException

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

+0

它在R中注册,就像public static final int imageViewLine = 0x7f060008;并且我只有一个xml布局 – 2013-03-21 19:01:15

+0

实际上,您在第二次查看该代码并使用更完整的代码后不会只有一个。你也有一个setContentView(drawView);在你的代码中。在您的onCreate()中通过init获取imageViewLine之前调用它。 – 2013-03-21 19:40:48

1

尝试在您的findViewById方法之前删除this.。也许你现在不在你的活动范围内。

+0

如果他不在Activity环境中,它不会编译(除非他在一个视图中) – 2013-03-21 19:12:34

+1

我说“试试...”,如果它有帮助,它会为他节省很多头痛:) – 2013-03-21 19:13:46

+0

那么..我将其删除,但我收回了同样的错误 – 2013-03-21 19:22:46