2013-03-21 2 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

'imageViewLine'이라는 이미지 뷰가 없습니다 –

+0

android : id = "@ + id/imageViewLine"? –

+0

그래. 액티비티에서로드하는 것과 동일한 XML 파일에 있는지 확인하십시오. –

답변

0

실제로 이미지를 사용하기 전에, 올바른 레이아웃에서 자원 R.id.imageViewLine를 참조하고 있는지 확인하십시오.

가끔 우리는 현재 레이아웃에서 사용할 수 있다고 생각되는 리소스의 인스턴스를 발견하지만 실제로는 현재 활성 레이아웃과 다른 레이아웃의 일부입니다. 일단 우리가 첫 속성을 설정하려고하면 NullPointerException이 발생합니다.

편집 : 코드 내에서 다른 현재 레이아웃을 설정하는 것처럼 보입니다. 이제 추가 코드가 도입되었습니다. 이것은 여기에 문제가 될 수 있습니다. 이것은 호출 스택 추적 onCreate(); ->initContextMenus(); ->setContentView(drawView); 에서 찾을 수 있습니다. 이는 ImageView 검색을 방해 할 수 있습니다.

+0

R에 등록되어 있습니다. public static final int imageViewLine = 0x7f060008; 그리고 나는 하나의 xml 레이아웃을 가지고있다. –

+0

실제로 두 번째로보고 난 후 더 완전한 코드로 하나만 가지고있다. 당신은 또한 setContentView (drawView); 귀하의 코드입니다. onCreate()에서 init을 통해 imageViewLine을 가져 오기 전에 호출 할 항목. –

1

findViewById 전에 this.을 제거해보세요. 어쩌면 당신은 현재 당신의 활동 컨텍스트에 있지 않습니다.

+0

그가 Activity 컨텍스트에 없다면 컴파일하지 않을 것입니다 (보기에 있지 않은 한) –

+1

"시도 ..."라고 말하면 도움이된다면 두통을 많이 줄일 수 있습니다.) –

+0

잘 .. 나는 그것을 제거하지만 나는 같은 오류를 암송합니다. –

관련 문제