我试图创建一个跟踪锻炼的集合和代表的锻炼应用程序的模拟。
以下是布局的外观:
我尝试做的是当我按下NEW时,它会创建一个新的练习实例。练习包含一组集合。每个集合都包含一个名为Reps的整数变量。我想要做的是创建一个Exercise对象(在这个例子中是一个pullup)然后当我点击NEW它会创建一个新的Set来添加到列表中,当我按下TAP按钮它将更新集合的Rep值。所以最终我想得到一个集合列表,每个集合都有一个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);
}
}
不幸的是,当我点击“新建”按钮时,应用程序崩溃了。
这是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)
我做错了什么?我的代码的逻辑是否合适?
答案 0 :(得分:0)
由于pullup._sets.add(tempSet)
行会导致错误,因此有两种可能的罪魁祸首:pullup
或_sets
可能是null
。进一步检查代码表明后者导致了问题。
要修复错误,您应该更改
public List<Sets> _sets;
到
private List<Sets> _sets = new ArrayList<Sets>();
请注意,我正在制作_sets
变量private
。这意味着其他类只能通过Exercise
类的方法访问此变量。这被认为是一种很好的编程习惯。我建议您了解有关封装和数据隐藏的更多信息,以便更好地理解它。
与错误更直接相关的是我正在为ArrayList
创建一个_sets
对象来引用。这是至关重要的,因为否则_sets
为null
,这意味着它不会引用任何有效对象,从而导致您看到的NullPointerException
。