我正在尝试为Android编写基本绘图Android应用程序并在Genymotion上测试它,但我无法点击应用程序图标(它显示“不幸[app]已经停止。”)
我不确定这三个私有变量是否应该在MainActivity类下?或者它与我的AndroidManifest.xml文件有关。有人可以查看我的代码吗?
我的MainActivity.java文件:
package edu.berkeley.cs160.opalkale.prog2;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private Paint myPaint;
private ImageButton currPaint;
private myView drawView;
public void paintClicked(View view) {
if (view != currPaint) {
// update color
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();
drawView.setColor(color);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
currPaint = (ImageButton)view;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView mV = new myView(this, null);
setContentView(mV);
drawView = (myView) findViewById(R.id.drawing);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.paint_colors);
currPaint = (ImageButton) paintLayout.getChildAt(0);
currPaint.setImageDrawable(getResources().getDrawable(
R.drawable.paint_pressed));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.berkeley.cs160.opalkale.prog2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="edu.berkeley.cs160.opalkale.prog2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
[2014-02-11 23:04:43 - DeviceMonitor] Adb connection Error:EOF
[2014-02-11 23:04:43 - DeviceMonitor] Sending jdwp tracking request failed!
[2014-02-11 23:04:43 - DeviceMonitor] Connection attempts: 1
[2014-02-11 23:04:44 - DeviceMonitor] Connection attempts: 2
[2014-02-11 23:04:45 - DeviceMonitor] Connection attempts: 3
[2014-02-11 23:04:46 - DeviceMonitor] Connection attempts: 4
[2014-02-11 23:04:47 - DeviceMonitor] Connection attempts: 5
[2014-02-11 23:04:48 - DeviceMonitor] Connection attempts: 6
[2014-02-11 23:04:49 - DeviceMonitor] Connection attempts: 7
[2014-02-11 23:04:50 - DeviceMonitor] Connection attempts: 8
[2014-02-11 23:04:51 - DeviceMonitor] Connection attempts: 9
[2014-02-11 23:04:52 - DeviceMonitor] Connection attempts: 10
[2014-02-11 23:04:53 - DeviceMonitor] Connection attempts: 11
[2014-02-11 23:07:14 - Logcat] device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:774)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:396)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:347)
at com.android.ddmlib.Device.executeShellCommand(Device.java:444)
at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:531)
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:screenOrientation="portrait"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCCCCCC"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/new_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="@string/brush"
android:src="@drawable/brush" />
</LinearLayout>
<edu.berkeley.cs160.opalkale.prog2.myView
android:id="@+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/paint_colors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:layout_width="@dimen/large_brush"
android:layout_height="@dimen/large_brush"
android:layout_margin="2dp"
android:background="#FF787878"
android:contentDescription="@string/paint"
android:onClick="paintClicked"
android:src="@drawable/paint"
android:tag="#FF787878" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
myView类:
package edu.berkeley.cs160.opalkale.prog2;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class myView extends View {
private Path drawPath;
private Paint drawPaint, canvasPaint;
private int paintColor = 0xFF787878;
private Canvas drawCanvas;
private Bitmap canvasBitmap;
public void setColor(String newColor){
//set color
invalidate();
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
private void setupDrawing() {
drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// view given size
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
// draw view
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// detect user touch
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
invalidate();
return true;
}
}
答案 0 :(得分:2)
setContentView(Id);
。因此,它会为您提供NullPointerException
将部分视图设置为您的活动,如
setContentView(R.layout.layout);
答案 1 :(得分:1)
NPE-空指针异常。没有调用setContentView。
另外,
drawView = (myView) findViewById(R.id.drawing);
你的myView是什么?
请将myView设置为您的活动视图,或使用R.id.drawing所在的布局调用setContentView。
答案 2 :(得分:1)
这只是一个常见问题,显示您的程序“遗憾的是,应用已停止”可能是由于Manifest file
引起的,因为您有两个类:
所以使用这个:
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".myView"
android:label="@string/app_name">
</activity>
</application>
答案 3 :(得分:1)
您的layout
在哪里。您的layout
遗失了。使用setContentView(R.layout.yourlayout);
答案 4 :(得分:1)
只需从此行中删除评论,
// setContentView(mV);