簡體   English   中英

“不幸的是,應用程序已停止”錯誤?

[英]“Unfortunately app has stopped” error?

我正在嘗試為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;
}

}

setContentView(Id); 您的代碼中缺少它。因此它給了您NullPointerException

為您的活動設置一些視圖,例如

setContentView(R.layout.layout);

NPE-空指針異常。 沒有調用setContentView。

也,

drawView = (myView) findViewById(R.id.drawing);  

您的myView是什么?

請將myView設置為您的活動的視圖,或使用R.id.drawing所在的布局調用setContentView。

這是Manifest file的常見問題,因為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>

哪里是你的layout 。您的layout丟失。 使用setContentView(R.layout.yourlayout);

只需刪除此行中的評論,

// setContentView(mV);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM