アプリをデバッグするとエラーが発生します。
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 ファイルが 1 つだけあり、そこにすべての画像があります。
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>