2012-07-28 2 views
-2

이것은 내 코드입니다. 라인 번호. 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 

} 

}

로그 캣 : get_all_ids에서

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 
+7

Main.java의 94 행에 get_all_ids() 메소드의 코드를 게시 할 수 있습니까? – Tushar

+2

Kan Kajal은 관련 코드를 게시하지 않으면 괜찮은 대답을 드릴 수 없습니다. –

+0

내 질문을 내 코드로 업데이트했습니다. 불편을 끼쳐 드려 죄송합니다. –

답변

1

잘못 가져 오는하는 조회수().

보기를 얻으려면, 당신이해야합니다

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을 읽을 것을 권장합니다.

관련 문제