2015-06-27 135 views
0

我正在嘗試創建一個跟蹤練習集和代表的鍛鍊應用程序的模擬。應用程序在點擊按鈕時崩潰

這裏是佈局的外觀:

enter image description here

我想要做的是,當我按新的IT創造鍛鍊的新實例而成。練習包含集合列表。每個集合都包含一個稱爲Reps的整數變量,我想要做的是創建一個Exercise對象(在這個例子中是一個pullup),然後當我點擊NEW時,它會創建一個新的Set添加到列表中,當我按下TAP按鈕它將更新該組的Rep值。所以最終我想最終得到一個集合列表,每個集合都有一個代表值。

例如:上拉:(5)(6)(5)(5)(3) 這是一個上拉五套,第一套有5代表,第二套有6代表,第三代有5代表代表等。

這是我寫的代碼:

Sets.java

package com.example.soufin.pullupsv3; 

/** 
* Created by Soufin on 6/26/2015. 
*/ 
public class Sets { 

    // private int _id; 
    private int _reps; 

    //constructor 
    //Sets(int reps) { 
    // setReps(reps); 
    //} 

    //getter 
    public int getReps() 
    { 
    return this._reps; 
    } 


    //setter 
    public void setReps(int reps) 
    { 
    this._reps = reps; 
    } 

} 

Exercise.java

package com.example.soufin.pullupsv3; 


import java.util.List; 
import com.example.soufin.pullupsv3.Sets; 

/** 
* Created by Soufin on 6/26/2015. 
*/ 
public class Exercise { 

    //public int ID; 
    public String _name; 
    public List<Sets> _sets; 

    //getter 
    public String getName() 
    { 
     return this._name; 
    } 
    //setter 
    public void setName(String name) 
    { 
     this._name = name; 
    } 

    //getter 
    public List getSets() {return this._sets; } 
    // setter 
    //public void setSets(int reps){this._sets.add(new Sets());} 




} 

Workout.java(主文件)

package com.example.soufin.pullupsv3; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.R.*; 

import com.example.soufin.pullupsv3.Sets; 
import com.example.soufin.pullupsv3.Exercise; 

import org.w3c.dom.Text; 


public class Workout extends ActionBarActivity { 

    Exercise pullup; 
    Button wNew; 
    Button wTap; 
    Button wEnd; 
    TextView displayText; 
    int newIndex; 
    int tapCount; 
    int[] score = new int[5]; 
    boolean flag = false; 
    String result; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_workout); 

     pullup = new Exercise(); 
     pullup.setName("Pullup"); 

     wNew = (Button) findViewById(R.id.newSetButton); 
     wTap = (Button) findViewById(R.id.tapButton); 
     wEnd = (Button) findViewById(R.id.endWorkoutButton); 
     displayText = (TextView) findViewById(R.id.displayScore); 


     wNew.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Sets tempSet = new Sets(); // create new Set on click of mNew 

       wTap.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         tapCount += 1; // increment based on number of taps 
        } 
       }); 

       tempSet.setReps(tapCount); 
       pullup._sets.add(tempSet); // add Set with reps (by taps) into List in Exercise 
       result = pullup.getSets().toString(); //store result 

      } 
     }); 

     displayText.setText(result); //display result 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_workout, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

不幸的是,當我點擊N EW按鈕,應用程序崩潰。

這裏是logcat的說:

06-27 17:36:09.499 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ Late-enabling CheckJNI 
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 409: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 431: Landroid/content/res/TypedArray;.getType (I)I 
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
06-27 17:36:09.919 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 140K, 7% free 3308K/3520K, paused 15ms, total 16ms 
06-27 17:36:09.935 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 4K, 6% free 3524K/3744K, paused 7ms, total 8ms 
06-27 17:36:10.011 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm-heap﹕ Grow heap (frag case) to 5.927MB for 2536932-byte allocation 
06-27 17:36:10.019 1236-1242/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 6001K/6224K, paused 6ms, total 6ms 
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libEGL_xap.so 
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - management hostname is 10.71.34.101 
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - connecting host: 10.71.34.1 
06-27 17:36:10.171 1236-1236/com.example.soufin.pullupsv3 D/﹕ HostConnection::get() New Host Connection established 0xb8786ca8, tid 1236 
06-27 17:36:10.171 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_xap.so 
06-27 17:36:10.175 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv2_xap.so 
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented 
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 D/OpenGLRenderer﹕ Enabling debug mode 0 
06-27 17:36:15.815 1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented 
06-27 17:36:19.395 1236-1236/com.example.soufin.pullupsv3 D/AndroidRuntime﹕ Shutting down VM 
06-27 17:36:19.395 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d6ab20) 
06-27 17:36:19.403 1236-1236/com.example.soufin.pullupsv3 E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.soufin.pullupsv3, PID: 1236 
    java.lang.NullPointerException 
      at com.example.soufin.pullupsv3.Workout$1.onClick(Workout.java:59) 
      at android.view.View.performClick(View.java:4438) 
      at android.view.View$PerformClick.run(View.java:18422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 

我在做什麼錯?我的代碼的邏輯是否正確?

+2

看起來你的ID爲'R.id.newSetButton'的視圖不包含佈局。添加'activity_workout.xml'內容以發佈。 –

+1

那麼,Workout.java中的第59行是什麼?如果它是'wTap.setOnClickListener',檢查佈局中是否有佈局爲''tapButton''的元素 –

+0

[可能是空指針異常,以及如何解決它?]的重複(http://stackoverflow.com/questions/ 218384/what-is-null-pointer-exception-and-how-do-i-fix-it) – njzk2

回答

0

由於符合pullup._sets.add(tempSet)的行會導致錯誤,因此有兩種可能的元兇:pullup_sets可能是null。進一步檢查你的代碼表明後面的代碼導致了這個問題。

要修正此錯誤,你應該改變

public List<Sets> _sets; 

private List<Sets> _sets = new ArrayList<Sets>(); 

注意,我作出_sets變量private。這意味着其他類只能通過Exercise類的方法訪問此變量。這被認爲是一種很好的編程習慣。我建議你更多地瞭解封裝和數據隱藏,以便更好地理解這一點。

與錯誤更直接相關的是,我正在爲_sets創建一個ArrayList對象來引用。這很關鍵,因爲否則_setsnull,這意味着它不引用任何有效的對象,然後導致您看到的NullPointerException

+0

完全同意你對我的回答的評論,所以我刪除了它,所以我不推銷壞習慣 – isma3l

+0

謝謝!你的解釋真的有幫助!得到它停止穿越。然而,我的顯示邏輯不工作?如何去更新我的TextView來顯示一個Pullup的對象,其中有一個對象集合裏面有一個Reps的int對象? – FlameDra

相關問題