2016-12-05 7 views
-3

アルファベットのストロークに従って、アルファベットを描画する子供用のアプリケーションを開発しています。 DrawingActivityはjava.lang.NullPointerExceptionによって実行できません。コーディングではあまり強くありません。私はgetIntent().getExtras().getString("type");からnullエラーが発生したと感じます。コードをgetIntent().getStringExtra("type");に変更しましたが、引き続きDrawingActivityを実行できません。私を助けてください。心から感謝する。次の原因でアクティビティを開くことができません:java.lang.NullPointerException

これは私のエラーlogcatです:

12-05 23:47:29.498 9620-9620/com.example.user.mygame E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.user.mygame, PID: 9620 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.mygame/com.example.user.mygame.DrawingActivity}: java.lang.NullPointerException 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5111) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
                      at dalvik.system.NativeStart.main(Native Method) 
                     Caused by: java.lang.NullPointerException 
                      at com.example.user.mygame.DrawingActivity.onCreate(DrawingActivity.java:156) 
                      at android.app.Activity.performCreate(Activity.java:5248) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2247)  
                      at android.app.ActivityThread.access$800(ActivityThread.java:141)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:136)  
                      at android.app.ActivityThread.main(ActivityThread.java:5111)  
                      at java.lang.reflect.Method.invokeNative(Native Method)  
                      at java.lang.reflect.Method.invoke(Method.java:515)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)  
                      at dalvik.system.NativeStart.main(Native Method)  

これは私のDrawingActivity.javaです:

package com.example.user.mygame; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Paint.Cap; 
import android.graphics.Paint.Join; 
import android.graphics.Paint.Style; 
import android.graphics.Path; 
import android.graphics.Path.Direction; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RelativeLayout.LayoutParams; 
import com.google.android.gms.location.DetectedActivity; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 

public class DrawingActivity extends Activity implements OnClickListener, OnTouchListener { 

    View drawingView; 
    DrawingView dv; 
    LayoutParams params; 
    ViewGroup parent; 
    ImageView nextBtn; 
    ImageView playBtn; 
    ImageView prevBtn; 
    private Paint mPaint; 
    private Integer position; 
    private int totalItem; 
    private String type; 

    public class DrawingView extends View { 
     private static final float TOUCH_TOLERANCE = 4.0f; 
     private Bitmap bm; 
     private Paint circlePaint; 
     private Path circlePath; 
     Context context; 
     private Bitmap mBitmap; 
     private Paint mBitmapPaint; 
     private Canvas mCanvas; 
     private Path mPath; 
     private float mX; 
     private float mY; 

     public DrawingView(Context c) { 
      super(c); 
      this.context = c; 
      this.mPath = new Path(); 
      this.mBitmapPaint = new Paint(4); 
      this.circlePaint = new Paint(); 
      this.circlePath = new Path(); 
      this.circlePaint.setAntiAlias(true); 
      this.circlePaint.setColor(Color.BLACK); 
      this.circlePaint.setStyle(Style.STROKE); 
      this.circlePaint.setStrokeJoin(Join.MITER); 
      this.circlePaint.setStrokeWidth(TOUCH_TOLERANCE); 
     } 

     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
      this.mBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); 
      this.mCanvas = new Canvas(this.mBitmap); 
      if (DrawingActivity.this.type.equals(Resource.DRAWING_ALPHABET)) { 
       this.bm = BitmapFactory.decodeResource(getResources(), Resource.capitalStoke[DrawingActivity.this.position]); 
      } 
      this.mCanvas.drawBitmap(this.bm, new Rect(0, 0, this.bm.getWidth(), this.bm.getHeight()), new Rect(0, 0, this.mCanvas.getWidth(), this.mCanvas.getHeight()), this.mBitmapPaint); 
     } 

     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      canvas.drawBitmap(this.mBitmap, 0.0f, 0.0f, this.mBitmapPaint); 
      canvas.drawPath(this.mPath, DrawingActivity.this.mPaint); 
      canvas.drawPath(this.circlePath, this.circlePaint); 
     } 

     private void touch_start(float x, float y) { 
      this.mPath.reset(); 
      this.mPath.moveTo(x, y); 
      this.mX = x; 
      this.mY = y; 
     } 

     private void touch_move(float x, float y) { 
      float dx = Math.abs(x - this.mX); 
      float dy = Math.abs(y - this.mY); 
      if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
       this.mPath.quadTo(this.mX, this.mY, (this.mX + x)/2.0f, (this.mY + y)/2.0f); 
       this.mX = x; 
       this.mY = y; 
       this.circlePath.reset(); 
       this.circlePath.addCircle(this.mX, this.mY, BitmapDescriptorFactory.HUE_ORANGE, Direction.CW); 
      } 
     } 

     private void touch_up() { 
      this.mPath.lineTo(this.mX, this.mY); 
      this.circlePath.reset(); 
      this.mCanvas.drawPath(this.mPath, DrawingActivity.this.mPaint); 
      this.mPath.reset(); 
     } 

     public boolean onTouchEvent(MotionEvent event) { 
      float x = event.getX(); 
      float y = event.getY(); 
      switch (event.getAction()) { 
       case DetectedActivity.IN_VEHICLE /*0*/: 
        touch_start(x, y); 
        invalidate(); 
        break; 
       case DetectedActivity.ON_BICYCLE /*1*/: 
        touch_up(); 
        invalidate(); 
        break; 
       case DetectedActivity.ON_FOOT /*2*/: 
        touch_move(x, y); 
        invalidate(); 
        break; 
      } 
      return true; 
     } 

     public void resetCanvas() { 
      this.bm = null; 
      this.mBitmap = null; 
      System.gc(); 
     } 
    } 

    public DrawingActivity() { 
     this.type = ""; 
     this.position = 0; 
     this.totalItem = 0; 

     this.nextBtn = null; 
     this.playBtn = null; 
     this.prevBtn = null; 
     this.drawingView = null; 
     this.parent = null; 
     this.params = null; 
    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(1); 
     setContentView(R.layout.drawing_alphabet); 
     this.type = getIntent().getExtras().getString("type"); 
     this.nextBtn = (ImageView) findViewById(R.id.nextId); 
     this.playBtn = (ImageView) findViewById(R.id.playId); 
     this.prevBtn = (ImageView) findViewById(R.id.prevId); 
     this.nextBtn.setOnClickListener(this); 
     this.nextBtn.setOnTouchListener(this); 
     this.prevBtn.setOnClickListener(this); 
     this.prevBtn.setOnTouchListener(this); 
     this.playBtn.setOnClickListener(this); 
     this.playBtn.setOnTouchListener(this); 
     this.drawingView = findViewById(R.id.drawingViewId); 
     this.params = (LayoutParams) this.drawingView.getLayoutParams(); 
     this.dv = new DrawingView(this); 
     this.dv.setLayoutParams(this.params); 
     this.parent = (ViewGroup) this.drawingView.getParent(); 
     int index = this.parent.indexOfChild(this.drawingView); 
     this.parent.removeView(this.drawingView); 
     this.parent.addView(this.dv, index); 
     if (this.type.equals(Resource.DRAWING_ALPHABET)) { 
      this.totalItem = Resource.capitalStoke.length; 
     } 
     this.mPaint = new Paint(); 
     this.mPaint.setAntiAlias(true); 
     this.mPaint.setDither(true); 
     this.mPaint.setColor(Color.BLACK); 
     this.mPaint.setStyle(Style.STROKE); 
     this.mPaint.setStrokeJoin(Join.ROUND); 
     this.mPaint.setStrokeCap(Cap.ROUND); 
     this.mPaint.setStrokeWidth(16.0f); 
     updatePreviousButton(); 
    } 


    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
      case DetectedActivity.IN_VEHICLE /*0*/: 
       if (v.getId() == R.id.nextId || v.getId() == R.id.playId || v.getId() == R.id.prevId) { 
        v.setAlpha(0.5f); 
        break; 
       } 
      case DetectedActivity.ON_BICYCLE /*1*/: 
       if (v.getId() == R.id.nextId || v.getId() == R.id.playId || v.getId() == R.id.prevId) { 
        v.setAlpha(1.0f); 
        break; 
       } 
     } 
     return false; 
    } 

    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.nextId: 
       this.position = this.position + 1; 
       changeStroke(); 
      case R.id.playId: 
       changeStroke(); 
      case R.id.prevId: 
       this.position = this.position - 1; 
       changeStroke(); 
      default: 
     } 
    } 

    private void changeStroke() { 
     updateNextButton(); 
     updatePreviousButton(); 
     int index = this.parent.indexOfChild(this.dv); 
     this.dv.resetCanvas(); 
     this.dv = null; 
     this.parent.removeViewAt(index); 
     this.dv = new DrawingView(this); 
     this.dv.setLayoutParams(this.params); 
     this.parent.addView(this.dv, index); 
    } 

    private void updateNextButton() { 
     if (this.position == this.totalItem - 1) { 
      this.nextBtn.setAlpha(0.5f); 
      this.nextBtn.setClickable(false); 
      return; 
     } 
     this.nextBtn.setAlpha(1.0f); 
     this.nextBtn.setClickable(true); 
    } 

    private void updatePreviousButton() { 
     if (this.position == 0) { 
      this.prevBtn.setAlpha(0.5f); 
      this.prevBtn.setClickable(false); 
      return; 
     } 
     this.prevBtn.setAlpha(1.0f); 
     this.prevBtn.setClickable(true); 
    } 
} 

これは私のdrawing_alphabet.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg_drawing"> 
    <View 
     android:id="@+id/drawingViewId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="0dp" 
     android:layout_marginRight="64dp" 
     android:layout_marginEnd="64dp"/> 
     <ImageView 
      android:id="@+id/prevId" 
      android:layout_width="52dp" 
      android:layout_height="52dp" 
      android:src="@drawable/back_icon" 
      android:layout_gravity="top" 
      android:layout_marginTop="49dp" 
      android:layout_alignParentTop="true" 
      android:layout_alignLeft="@+id/nextId" 
      android:layout_alignStart="@+id/nextId" /> 
     <ImageView 
      android:id="@+id/nextId" 
      android:layout_width="52dp" 
      android:layout_height="52dp" 
      android:src="@drawable/next_icon" 
      android:layout_gravity="bottom" 
      android:layout_marginTop="61dp" 
      android:layout_below="@+id/prevId" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" />/

</RelativeLayout> 

は、これが私のマニフェストです。 xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.user.mygame"> 

    <application 
     android:allowBackup="false" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     android:name="android.support.multidex.MultiDexApplication"> 
     <activity android:name=".MainActivity" 
      android:screenOrientation="landscape"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:label="@string/app_name" android:name="com.example.user.mygame.AlphabetActivity" android:screenOrientation="landscape" /> 
     <activity android:label="@string/app_name" android:name="com.example.user.mygame.DrawingActivity" android:screenOrientation="landscape" /> 
    </application> 

</manifest> 

これは私のresource.javaです:

package com.example.user.mygame; 


public class Resource { 

    public static String DRAWING_ALPHABET; 
    public static Integer[] capitalStoke; 
    Integer[] alphabetCapital; 
    Integer[] alphabetSound; 
    Integer[] alphabetImage; 

    static { 
     DRAWING_ALPHABET = "alphabet"; 
     setCapitalStoke(new Integer[]{Integer.valueOf(R.drawable.capital_letters_stroke_01), 
       Integer.valueOf(R.drawable.capital_letters_stroke_02), 
       Integer.valueOf(R.drawable.capital_letters_stroke_03), 
       Integer.valueOf(R.drawable.capital_letters_stroke_04), 
       Integer.valueOf(R.drawable.capital_letters_stroke_05), 
       Integer.valueOf(R.drawable.capital_letters_stroke_06), 
       Integer.valueOf(R.drawable.capital_letters_stroke_07), 
       Integer.valueOf(R.drawable.capital_letters_stroke_08), 
       Integer.valueOf(R.drawable.capital_letters_stroke_09), 
       Integer.valueOf(R.drawable.capital_letters_stroke_10), 
       Integer.valueOf(R.drawable.capital_letters_stroke_11), 
       Integer.valueOf(R.drawable.capital_letters_stroke_12), 
       Integer.valueOf(R.drawable.capital_letters_stroke_13), 
       Integer.valueOf(R.drawable.capital_letters_stroke_14), 
       Integer.valueOf(R.drawable.capital_letters_stroke_15), 
       Integer.valueOf(R.drawable.capital_letters_stroke_16), 
       Integer.valueOf(R.drawable.capital_letters_stroke_17), 
       Integer.valueOf(R.drawable.capital_letters_stroke_18), 
       Integer.valueOf(R.drawable.capital_letters_stroke_19), 
       Integer.valueOf(R.drawable.capital_letters_stroke_20), 
       Integer.valueOf(R.drawable.capital_letters_stroke_21), 
       Integer.valueOf(R.drawable.capital_letters_stroke_22), 
       Integer.valueOf(R.drawable.capital_letters_stroke_23), 
       Integer.valueOf(R.drawable.capital_letters_stroke_24), 
       Integer.valueOf(R.drawable.capital_letters_stroke_25), 
       Integer.valueOf(R.drawable.capital_letters_stroke_26)}); 

    } 

    public static Integer[] getCapitalStoke() { 
     return capitalStoke; 
    } 
    public static void setCapitalStoke(Integer[] capitalStoke) { 
     Resource.capitalStoke = capitalStoke; 
    } 

    public Resource() { 
     this.alphabetCapital = new Integer[]{ 
       Integer.valueOf(R.drawable.img_letter_001), 
       Integer.valueOf(R.drawable.img_letter_002), 
       Integer.valueOf(R.drawable.img_letter_003), 
       Integer.valueOf(R.drawable.img_letter_004), 
       Integer.valueOf(R.drawable.img_letter_005), 
       Integer.valueOf(R.drawable.img_letter_006), 
       Integer.valueOf(R.drawable.img_letter_007), 
       Integer.valueOf(R.drawable.img_letter_008), 
       Integer.valueOf(R.drawable.img_letter_009), 
       Integer.valueOf(R.drawable.img_letter_010), 
       Integer.valueOf(R.drawable.img_letter_011), 
       Integer.valueOf(R.drawable.img_letter_012), 
       Integer.valueOf(R.drawable.img_letter_013), 
       Integer.valueOf(R.drawable.img_letter_014), 
       Integer.valueOf(R.drawable.img_letter_015), 
       Integer.valueOf(R.drawable.img_letter_016), 
       Integer.valueOf(R.drawable.img_letter_017), 
       Integer.valueOf(R.drawable.img_letter_018), 
       Integer.valueOf(R.drawable.img_letter_019), 
       Integer.valueOf(R.drawable.img_letter_020), 
       Integer.valueOf(R.drawable.img_letter_021), 
       Integer.valueOf(R.drawable.img_letter_022), 
       Integer.valueOf(R.drawable.img_letter_023), 
       Integer.valueOf(R.drawable.img_letter_024), 
       Integer.valueOf(R.drawable.img_letter_025), 
       Integer.valueOf(R.drawable.img_letter_026)}; 

     this.alphabetSound = new Integer[]{ 
       Integer.valueOf(R.raw.snd_a), 
       Integer.valueOf(R.raw.snd_b), 
       Integer.valueOf(R.raw.snd_c),}; 

     this.alphabetImage = new Integer[]{ 
       Integer.valueOf(R.drawable.image_1), 
       Integer.valueOf(R.drawable.image_2), 
       Integer.valueOf(R.drawable.image_3)}; 
    } 

} 
+0

を設定しなかったこのhttp://stackoverflow.com/questions/20402767/fatal-exception-main-unable-to-start-activity-componentinfoに確認してください。 -caused-by-java-lan?rq = 1 – lsof

+0

[mcve] –

答えて

1

NullPointerExceptionが指摘するように、あなたのエラーはのonCreateメソッドでDrawingActivityという名前のファイルにライン156上にあるように思えます。私はこれが断層運動されている行であると信じる

this.type = getIntent().getExtras().getString("type"); 

は、その中の値として「タイプ」と意図を作成するために、ここであなたを送信している活動を再確認してください。 DrawingActivityを呼ぶ活動に

0

、あなたが

Intent intent = new Intent(context, DrawingActivity.class); 
intent.putExtra("type","anyString"); // don't forget this line 
startActivity(intent); 
+0

Nopeの作成方法を参照してください。私はthis.type = intent.putExtra( "type"、 "anyString")を設定する必要がありますか? DrawingActivity()で? –

+0

申し訳ありませんが、よく理解できません。 –

+0

はDrawingActivityではなく、開始したアクティビティです。 DrawingActivityがマニフェストで定義されているアクティビティを開始している場合、インテントに余分なものを置くことはできません。 –

関連する問題