gpt4 book ai didi

java - "Unfortunately app has stopped"错误?

转载 作者:行者123 更新时间:2023-12-01 08:05:48 25 4
gpt4 key购买 nike

我正在尝试为 android 编写一个基本的绘图 Android 应用程序并在 Genymotion 上测试它,但我无法单击应用程序图标(它显示“不幸的是 [app] 已停止。”)

我不确定这三个私有(private)变量是否应该位于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>
<小时/>

日志猫:

[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;
}

}

最佳答案

您的代码中缺少

setContentView(Id);。因此它会给您 NullPointerException

为您的 Activity 设置一些 View ,例如

setContentView(R.layout.layout);

关于java - "Unfortunately app has stopped"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com