我被卡住了。我花了好幾個小時看着這個,我知道這一定是微不足道的,我做錯了。我的本地「舌頭」是C++,但我想與Android合作。我真的很喜歡在C++中使用結構,但發現我不得不在Java中使用類。我已經很好地縮小了,因爲我的班級出了問題,我得到了一個空指針。有任何想法嗎?爲什麼我會得到一個nullpointerexception,可能是類錯誤(Android)?
這裏是267線的代碼標記:
public class Vectors extends Activity{
Button next;
public class infoC{
double value = 0, angle = 0;
boolean radian = false; //radians/degrees
int quad = 1; //Quadrant
}
public infoC[] info;
int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vectors);
vectorsView = new VectorsView(this);
l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView);
Initialize();
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
error.setText("");
if(value.getText().toString().length()==0||value.getText()==null||angle.getText().toString().length()==0||angle.getText()==null)
error.setText("Must enter both value and angle");
else{
Log.e("Counter", Integer.toString(counter));
Log.e("Value", value.getText().toString());
storeInfo(); //121
counter++;
}
}
});
}
public void storeInfo(){
Doublify(value);
Doublify(angle);
String temp;
temp=value.getText().toString();
info[counter].value = Double.parseDouble(temp);//267, Inserted temp instead of getText.toString
info[counter].angle = Double.parseDouble(angle.getText().toString());
info[counter].radian = rad.isChecked();
if(q1.isChecked())
info[counter].quad=1;
if(q2.isChecked())
info[counter].quad=2;
if(q3.isChecked())
info[counter].quad=3;
if(q4.isChecked())
info[counter].quad=4;
angle.setText("");
value.setText("");
}
}
這裏是logcat的:
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): FATAL EXCEPTION: main
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): java.lang.NullPointerException
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.prattia.webs.cheaterphysics.Vectors.storeInfo(Vectors.java:267)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.prattia.webs.cheaterphysics.Vectors$7.onClick(Vectors.java:121)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.view.View.performClick(View.java:2465)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.view.View$PerformClick.run(View.java:8907)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Handler.handleCallback(Handler.java:587)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.os.Looper.loop(Looper.java:123)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at java.lang.reflect.Method.invoke(Method.java:521)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): at dalvik.system.NativeStart.main(Native Method)
什麼是Doublify在你的代碼 – 2012-04-14 02:20:13
好吧你們中的一些人是正確的...這是我缺乏結構化的Java培訓。我運行了一個for(循環初始化信息[i]作爲新的infoC()。 – rphello101 2012-04-14 02:25:56
現在是您的問題解決 – 2012-04-14 02:35:36