这是我的代码: 行号94对应于我获取所有ID的方法 我总是收到错误:您的应用程序意外停止。
package karan.app.caloriecalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Main extends Activity implements AdapterView.OnItemSelectedListener {
Button bcal;
TextView intro, choose, duration, min, weight, kg;
EditText dur, weigh;
Spinner sel;
String temp;
public double w, d, calories;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get_all_ids();
setArrayAdapter();
sel.setOnItemSelectedListener(this);
bcal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
w = Float.valueOf(weigh.getText().toString());
d = Float.valueOf(dur.getText().toString());
if (temp.contentEquals("Aerobics:general")){
calories = 0.11*w*d;
}
else if (temp.contentEquals("Badminton")){
calories = (w/13)*d;
}
else if (temp.contentEquals("Basketball")){
calories = (w/7.45)*d;
}
else if (temp.contentEquals("Bicycling:slow (10-12 mph)")){
calories = (w/9.65)*d;
}
else if (temp.contentEquals("Bicycling:general (12-14 mph)")){
calories = (w/7.2)*d;
}
else if (temp.contentEquals("Bicycling:moderate (14-16 mph)")){
calories = (w/6)*d;
}
else if (temp.contentEquals("Bicycling:fast (16+ mph)")){
calories = (w/5)*d;
}
else if (temp.contentEquals("Bowling")){
calories = (w/19.1)*d;
}
else if (temp.contentEquals("Boxing: punching bag")){
calories = (w/9.6)*d;
}
else if (temp.contentEquals("Boxing: sparring")){
calories = (w/6.375)*d;
}
/* Thread t = new Thread(){
public void run(){
try{
sleep(1);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent openResult = new Intent("karan.app.caloriecalculator.RESULT");
startActivity(openResult);
}
}
}; // close the thread with a ;
*/
}});
}
public void get_all_ids() {
bcal.findViewById(R.id.b1);
intro.findViewById(R.id.tv1);
choose.findViewById(R.id.tv2);
duration.findViewById(R.id.tv3);
min.findViewById(R.id.tv4);
weight.findViewById(R.id.tv5);
kg.findViewById(R.id.tv6);
dur.findViewById(R.id.et2);
weigh.findViewById(R.id.et1);
sel.findViewById(R.id.sp1);
}
public double getCalories() {
return calories;
}
public void setArrayAdapter(){
//The createFromResource() method allows you to create an ArrayAdapter from the string array
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.choices, android.R.layout.simple_spinner_dropdown_item);
//You should then call setDropDownViewResource(int) to specify the layout the adapter should use to display the list of spinner choices
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Call setAdapter() to apply the adapter to your Spinner.
sel.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Object item = parent.getItemAtPosition(position);
temp = item.toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
logcat的:
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.os.Looper.loop(Looper.java:123)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 14:10:03.545: E/AndroidRuntime(266): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 14:10:03.545: E/AndroidRuntime(266): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 14:10:03.545: E/AndroidRuntime(266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 14:10:03.545: E/AndroidRuntime(266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 14:10:03.545: E/AndroidRuntime(266): at dalvik.system.NativeStart.main(Native Method)
07-28 14:10:03.545: E/AndroidRuntime(266): Caused by: java.lang.NullPointerException
07-28 14:10:03.545: E/AndroidRuntime(266): at karan.app.caloriecalc.MainActivity.get_all_ids(MainActivity.java:96)
07-28 14:10:03.545: E/AndroidRuntime(266): at karan.app.caloriecalc.MainActivity.onCreate(MainActivity.java:28)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 14:10:03.545: E/AndroidRuntime(266): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 14:10:03.545: E/AndroidRuntime(266): ... 11 more
答案 0 :(得分:1)
您在get_all_ids()中错误地获取了视图。
要获得视图,您必须执行以下操作:
TextView intro;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intro = (TextView) findViewById(R.id.tv1);
....
}
// You can now use intro and methods of TextView class.
我建议你阅读Android Beginner Guides。