2015-06-27 60 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

相关问题