2012-08-14 58 views
0

所以我把這個項目放在幾個星期前,它完美的工作。我在今天早些時候對其中一個活動做了一些修改,將其加載到模擬器中,並在進入我的菜單屏幕(splash - > menu - > new activities)後死亡。所以在兩次崩潰之後,我嘗試了一個不同的活動。一個工作。回來......點擊另一個沒有被觸及的景氣,景氣。強制關閉/崩潰。我沒有更改菜單java或XML文件中的任何內容,也沒有更改清單。從菜單中點擊android強制關閉?附上日誌

世界上可能造成這種情況的是什麼?在Eclipse中沒有錯誤,我沒有改變任何東西,我想,導致它做到這一點。

日誌附:


08-14 14:34:45.301: W/KeyCharacterMap(343): No keyboard for id 0 
08-14 14:34:45.301: W/KeyCharacterMap(343): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
08-14 14:35:40.461: D/AndroidRuntime(343): Shutting down VM 
08-14 14:35:40.471: W/dalvikvm(343): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-14 14:35:40.491: E/AndroidRuntime(343): FATAL EXCEPTION: main 
08-14 14:35:40.491: E/AndroidRuntime(343): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tomcat.performance/com.tomcat.performance.TireTemps}: java.lang.NullPointerException 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.os.Looper.loop(Looper.java:123) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-14 14:35:40.491: E/AndroidRuntime(343): at java.lang.reflect.Method.invokeNative(Native Method) 
08-14 14:35:40.491: E/AndroidRuntime(343): at java.lang.reflect.Method.invoke(Method.java:521) 
08-14 14:35:40.491: E/AndroidRuntime(343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-14 14:35:40.491: E/AndroidRuntime(343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-14 14:35:40.491: E/AndroidRuntime(343): at dalvik.system.NativeStart.main(Native Method) 
08-14 14:35:40.491: E/AndroidRuntime(343): Caused by: java.lang.NullPointerException 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.Activity.findViewById(Activity.java:1637) 
08-14 14:35:40.491: E/AndroidRuntime(343): at com.tomcat.performance.TireTemps.<init>(TireTemps.java:25) 
08-14 14:35:40.491: E/AndroidRuntime(343): at java.lang.Class.newInstanceImpl(Native Method) 
08-14 14:35:40.491: E/AndroidRuntime(343): at java.lang.Class.newInstance(Class.java:1429) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
08-14 14:35:40.491: E/AndroidRuntime(343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 
08-14 14:35:40.491: E/AndroidRuntime(343): ... 11 more 

TIRETEMPS.JAVA

package com.tomcat.performance; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
package com.tomcat.performance; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class TireTemps extends Activity implements OnClickListener { 
    EditText tempLFouterString = ((EditText) findViewById(R.id.tempLFouter)), 
      tempLFmiddleString = ((EditText) findViewById(R.id.tempLFmiddle)), 
      tempLFinnerString = ((EditText) findViewById(R.id.tempLFinner)), 
      tempRFinnerString = ((EditText) findViewById(R.id.tempRFinner)), 
      tempRFmiddleString = ((EditText) findViewById(R.id.tempRFmiddle)), 
      tempRFouterString = ((EditText) findViewById(R.id.tempRFouter)), 
      tempRRinnerString = ((EditText) findViewById(R.id.tempRRinner)), 
      tempRRmiddleString = ((EditText) findViewById(R.id.tempRRmiddle)), 
      tempRRouterString = ((EditText) findViewById(R.id.tempRRouter)), 
      tempLRouterString = ((EditText) findViewById(R.id.tempLRouter)), 
      tempLRmiddleString = ((EditText) findViewById(R.id.tempLRmiddle)), 
      tempLRinnerString = ((EditText) findViewById(R.id.tempLRinner)); 

    TextView tempNoseAverageString = ((TextView) findViewById(R.id.tempNoseAverage)), 
      tempLFaverageString = ((TextView) findViewById(R.id.tempLFaverage)), 
      tempRFaverageString = ((TextView) findViewById(R.id.tempRFaverage)), 
      tempLRaverageString = ((TextView) findViewById(R.id.tempLRaverage)), 
      tempRRaverageString = ((TextView) findViewById(R.id.tempRRaverage)), 
      tempRearAverageString = ((TextView) findViewById(R.id.tempRearAverage)), 
      tempRFLRaverageString = ((TextView) findViewById(R.id.tempRFLRaverage)), 
      tempLFRRaverageString = ((TextView) findViewById(R.id.tempLFRRaverage)), 
      tempRSaverageString = ((TextView) findViewById(R.id.tempRSaverage)), 
      tempLSaverageString = ((TextView) findViewById(R.id.tempLSaverage)); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tiretemps); 

     Button calculate = ((Button) findViewById(R.id.calculate)); 
     calculate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       double tempLFouter, tempLFmiddle, tempLFinner, tempRFinner, tempRFmiddle, 
         tempRFouter, tempLRouter, tempLRmiddle, tempLRinner, tempRRinner, 
         tempRRmiddle, tempRRouter, tempNoseAverage, tempLFaverage, tempRFaverage, 
         tempLRaverage, tempRRaverage, tempRearAverage, tempRFLRaverage, tempLFRRaverage, 
         tempLSaverage, tempRSaverage; 

       try { 
        tempLFouter = Double.parseDouble(tempLFouterString.getText().toString()); 
        tempLFinner = Double.parseDouble(tempLFinnerString.getText().toString()); 
        tempLFmiddle = Double.parseDouble(tempLFmiddleString.getText().toString()); 
        tempRFouter = Double.parseDouble(tempRFouterString.getText().toString()); 
        tempRFmiddle = Double.parseDouble(tempRFmiddleString.getText().toString()); 
        tempRFinner = Double.parseDouble(tempRFinnerString.getText().toString()); 
        tempRRouter = Double.parseDouble(tempRRouterString.getText().toString()); 
        tempRRmiddle = Double.parseDouble(tempRRmiddleString.getText().toString()); 
        tempRRinner = Double.parseDouble(tempRRinnerString.getText().toString()); 
        tempLRouter = Double.parseDouble(tempLRouterString.getText().toString()); 
        tempLRmiddle = Double.parseDouble(tempLRmiddleString.getText().toString()); 
        tempLRinner = Double.parseDouble(tempLRinnerString.getText().toString()); 

        tempLFaverage = ((tempLFinner + tempLFmiddle + tempLFouter)/3); 
        tempRFaverage = ((tempRFinner + tempRFmiddle + tempRFouter)/3); 
        tempLRaverage = ((tempLRinner + tempLRmiddle + tempLRouter)/3); 
        tempRRaverage = ((tempRRinner + tempRRmiddle + tempRRouter)/3); 
        tempNoseAverage = ((tempLFaverage + tempRFaverage)/2); 
        tempRearAverage = ((tempLRaverage + tempRRaverage)/2); 
        tempRFLRaverage = ((tempRFaverage + tempLRaverage)/2); 
        tempLFRRaverage = ((tempLFaverage + tempRRaverage)/2); 
        tempLSaverage = ((tempLFaverage + tempLRaverage)/2); 
        tempRSaverage = ((tempRFaverage + tempRRaverage)/2); 

        tempNoseAverageString.setText(String.valueOf(String.format("%.1f", tempNoseAverage)) + "°"); 
        tempLFaverageString.setText(String.valueOf(String.format("%.1f", tempLFaverage)) + "°"); 
        tempRFaverageString.setText(String.valueOf(String.format("%.1f", tempRFaverage)) + "°"); 
        tempLRaverageString.setText(String.valueOf(String.format("%.1f", tempLRaverage)) + "°"); 
        tempRRaverageString.setText(String.valueOf(String.format("%.1f", tempRRaverage)) + "°"); 
        tempRearAverageString.setText(String.valueOf(String.format("%.1f", tempRearAverage)) + "°"); 
        tempRFLRaverageString.setText(String.valueOf(String.format("%.1f", tempRFLRaverage)) + "°"); 
        tempLFRRaverageString.setText(String.valueOf(String.format("%.1f", tempLFRRaverage)) + "°"); 
        tempLSaverageString.setText(String.valueOf(String.format("%.1f", tempLSaverage)) + "°"); 
        tempRSaverageString.setText(String.valueOf(String.format("%.1f", tempRSaverage)) + "°"); 

       } catch (NumberFormatException e) { 
        Toast andEggs = Toast 
          .makeText(TireTemps.this, 
          "Please verify only numbers are used in your inputs with no blank data.", 
          Toast.LENGTH_LONG); 
        andEggs.show(); 
       } catch (NullPointerException n) { 
        Toast andEggs = Toast.makeText(TireTemps.this, "Please enter all temperature data for each tire.", 
          Toast.LENGTH_LONG); 
        andEggs.show(); 
       } 
      } 
     }); 

     Button reset_temps = ((Button) findViewById(R.id.reset_temps)); 
     reset_temps.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       tempLFouterString.setText(""); 
       tempLFinnerString.setText(""); 
       tempLFmiddleString.setText(""); 
       tempRFouterString.setText(""); 
       tempRFmiddleString.setText(""); 
       tempRFinnerString.setText(""); 
       tempRRouterString.setText(""); 
       tempRRmiddleString.setText(""); 
       tempRRinnerString.setText(""); 
       tempLRouterString.setText(""); 
       tempLRmiddleString.setText(""); 
       tempLRinnerString.setText(""); 

       tempNoseAverageString.setText(""); 
       tempLFaverageString.setText(""); 
       tempRFaverageString.setText(""); 
       tempLRaverageString.setText(""); 
       tempRRaverageString.setText(""); 
       tempRearAverageString.setText(""); 
       tempRFLRaverageString.setText(""); 
       tempLFRRaverageString.setText(""); 
       tempLSaverageString.setText(""); 
       tempRSaverageString.setText(""); 

       Toast andEggs = Toast.makeText(TireTemps.this, "All data has been reset.", Toast.LENGTH_LONG); 
       andEggs.show(); 

      } 
     }); 
    } 

    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

編輯:添加TireTemps.java

+0

Post TireTemps.java:25和周圍的代碼。 – 2012-08-14 16:13:15

+0

我想你試圖引用TireTemps.java類中的null變量(在25行左右) – 2012-08-14 16:24:37

+0

我認爲你已經清理了這個項目? – WarrenFaith 2012-08-14 16:57:45

回答

1

findviewbyid後才工作你已經完成了一個setContentView。但是你正在類實例化中實例化所有的EditTexts。 findviewbyid無法知道在哪裏查找ID。

將您在字段實例化中完成的所有findviewbyid移動到Oncreate。

+0

當在onCreate中移動它們時,我會遇到有關引用最終/非最終變量的重大錯誤。有沒有辦法在後面的java中相對容易地引用這些變量?仍然是一個小菜,所以試圖在飛行中學習:)奇怪的是,它直到最近才完全正常工作。在我添加一些編輯之前,現在在我的手機上有一個工作版本....沒有一個在eclipse中彈出錯誤。 – 2012-08-20 14:10:13

+0

將變量聲明爲字段,然後在Oncreate中初始化它們 – nandeesh 2012-08-20 14:56:27

+0

不知道爲什麼它以前工作...但它確實。無論如何,所有的東西都在onCreate中重新初始化,並且沒有問題。謝謝! – 2012-08-20 15:39:08

0

Tiretemps.java線25訪問一個變量,該變量尚未實例化或爲空在訪問它。

這可以根據您如何聲明變量,你不把它們設置爲價值可言,或在您的其他生命週期功能,它們將被清除,如或onResume

+0

關於此問題的一個奇怪部分是CQM,該應用程序的這一部分在我最後一次使用它時非常完美。我沒有編輯或以其他方式觸摸該文件,現在這個。這就是爲什麼我很困惑。這個特定的部分是我想要的地方,沒有進一步的計劃編輯。 – 2012-08-14 17:13:58

+1

嘗試清理您的項目並運行。 – 2012-08-14 17:27:19