- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在调试应用程序时收到错误。
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 第一个 Activity 的完整代码。 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>
最佳答案
尝试删除 findViewById
方法之前的 this.
。也许您当前不在 Activity 上下文中。
关于java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15555805/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!