2014-02-12 56 views
0

我想为Android编写一个基本的绘图Android应用程序,并在Genymotion上测试它,但我无法点击应用程序图标(它说“不幸的是[app]已停止。”)“不幸的应用程序已停止”错误?

I' m不知道这三个私有变量应该在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; 
} 

}

+2

请发表您的logcat – user2450263

+2

的setContentView(MV);缺少代码 –

+0

@kalyanpvs即使没有注释掉,我仍然有同样的问题 – Opal

回答

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> 

+0

@HasAlTaiar相反,我只是取消注释myView mV = new myView(this,null);和setContentView(mV);,其中myView是一个扩展View的类。我仍然无法点击我的应用程序。任何指针? – Opal

+0

不,使用'setContentView(R.layout.activity_file_name);'否则你会得到一个错误.. :) –

+0

所以我应该把它改成'setContentView(R.layout.activity_main)',然后注释掉'myView mV = new myView(this,null);''和'drawView =(myView)findViewById(R.id.drawing);'? – Opal

2

setContentView(Id);中缺少您的code.So它给你NullPointerException

将一些视图设置为您的活动,如

setContentView(R.layout.layout); 
1

NPE-空指针异常。没有调用setContentView。

此外,

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

什么是您的MyView的?

请将myView设置为您的活动视图,或者使用R.id.drawing所在的布局调用setContentView。

1

哪里是你的layout。您的layout是米西NG。使用setContentView(R.layout.yourlayout);

1

刚刚从该行删除注释,

// setContentView(mV); 
相关问题