2013-02-07 81 views
0

我的應用程序工作沒有任何問題,直到關閉我的應用程序,超過15分鐘後返回aprox它崩潰,如果我只有5分鐘後返回到我的應用程序似乎一切正常。我找不到問題請幫忙!!!Android:ComponentInfo:java空指針異常返回到應用程序後

logcat的

02-06 20:39:43.534: E/AndroidRuntime(1851): FATAL EXCEPTION: main 
02-06 20:39:43.534: E/AndroidRuntime(1851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raigo.itivamin/com.raigo.itivamin.Bartabs}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raigo.itivamin/com.raigo.itivamin.ObjetivosActivity}: java.lang.NullPointerException 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.access$600(ActivityThread.java:127) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.os.Looper.loop(Looper.java:210) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.main(ActivityThread.java:4441) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at dalvik.system.NativeStart.main(Native Method) 
02-06 20:39:43.534: E/AndroidRuntime(1851): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raigo.itivamin/com.raigo.itivamin.ObjetivosActivity}: java.lang.NullPointerException 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1808) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:737) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.widget.TabHost.setCurrentTab(TabHost.java:401) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.widget.TabHost.addTab(TabHost.java:240) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.raigo.itivamin.Bartabs.addTab(Bartabs.java:65) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.raigo.itivamin.Bartabs.setTabs(Bartabs.java:37) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.raigo.itivamin.Bartabs.onCreate(Bartabs.java:29) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.Activity.performCreate(Activity.java:4465) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  ... 11 more 
02-06 20:39:43.534: E/AndroidRuntime(1851): Caused by: java.lang.NullPointerException 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at com.raigo.itivamin.ObjetivosActivity.onCreate(ObjetivosActivity.java:25) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.Activity.performCreate(Activity.java:4465) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
02-06 20:39:43.534: E/AndroidRuntime(1851):  ... 23 more 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.raigo.itivamin" 
    android:versionCode="10" 
    android:versionName="1.1" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <application android:name="com.raigo.itivamin.iTIVAapp" 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/app_theme" > 
     <activity 
      android:name=".IngresodatosActivity" 
      android:label="@string/app_name" > 
        <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name="Bartabs" android:configChanges="orientation|screenSize"></activity> 
     <activity android:name=".ObjetivosActivity" android:configChanges="orientation|screenSize"></activity> 
     <activity android:name=".BombaActivity" android:configChanges="orientation|screenSize"></activity> 
     <activity android:name=".OpioideActivity" 
      android:theme="@android:style/Theme.Dialog"></activity>  
     <activity android:name=".HipnoticoActivity" 
      android:theme="@android:style/Theme.Dialog"></activity> 
     <activity android:name="org.achartengine.GraphicalActivity" /> 
     <activity android:name=".GraficaActivity" android:configChanges="orientation|screenSize"></activity> 
     <activity android:name=".SimuladorActivity" android:configChanges="orientation|screenSize"></activity> 
     <activity android:name=".AyudaActivity" android:configChanges="orientation|screenSize"></activity> 

     <service android:name=".SomeService1"></service> 
    </application> 

</manifest> 

Bartabs.java

package com.raigo.itivamin; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TabHost; 
import android.widget.TextView; 


/** 
* @author Adil Soomro 
* 
*/ 
@SuppressWarnings("deprecation") 
public class Bartabs extends TabActivity { 
    /** Called when the activity is first created. */ 
//boolean simulador =false; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     setContentView(R.layout.main); 
     //Bundle extras = getIntent().getExtras(); 
     //simulador= extras.getBoolean("simulador"); 
     setTabs() ; 

    } 

    private void setTabs() 
    { 

    if (((iTIVAapp)getApplication()).simulador==false){   
     addTab(getString(R.string.target), R.drawable.tab_objetivos, ObjetivosActivity.class); 
     addTab(getString(R.string.pump), R.drawable.tab_bomba, BombaActivity.class); 
     addTab2(getString(R.string.pk_pd), R.drawable.tab_chart,GraficaActivity.class); 
     addTab(getString(R.string.help), R.drawable.tab_help, AyudaActivity.class); 
    } else{ 
     addTab(getString(R.string.scheme), R.drawable.tab_bomba, SimuladorActivity.class); 
     addTab2(getString(R.string.pk_pd), R.drawable.tab_chart,GraficaActivity.class); 
     addTab(getString(R.string.help), R.drawable.tab_help, AyudaActivity.class); 
    } 

    } 

    private void addTab(String labelId, int drawableId, Class<?> c) 
    { 
     TabHost tabHost = getTabHost(); 
     Intent intent = new Intent(this, c); 
     TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

     View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
     TextView title = (TextView) tabIndicator.findViewById(R.id.title); 

     title.setText(labelId); 
       ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
     icon.setImageResource(drawableId); 

     spec.setIndicator(tabIndicator); 
     spec.setContent(intent); 

     tabHost.addTab(spec); 
     //tabHost.setOnTabChangedListener(this); 

    } 

    private void addTab2(String labelId, int drawableId, Class<?> c) 
    { 
     TabHost tabHost = getTabHost(); 
     Intent intent = new Intent(this, c).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

     View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
     TextView title = (TextView) tabIndicator.findViewById(R.id.title); 

     title.setText(labelId); 
       ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
     icon.setImageResource(drawableId); 

     spec.setIndicator(tabIndicator); 
     spec.setContent(intent); 

     tabHost.addTab(spec); 
     //tabHost.setOnTabChangedListener(this); 
    } 


    //@Override 
    //public void onTabChanged(String tabId) { 

    // if (tabId.equals("tabPK/PD")==true && (((iTIVAapp)getApplication()).cambio==true)){ 
    // LocalActivityManager manager = getLocalActivityManager(); 
    // manager.destroyActivity("tabPK/PD", true); 
    // manager.startActivity("tabPK/PD", new Intent(this, GraficaActivity.class)); 
    // } 
    //} 


} 

ObjetivosActivity.java

package com.raigo.itivamin; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.NumberPicker; 
import android.widget.NumberPicker.OnValueChangeListener; 
import android.widget.Toast; 

public class ObjetivosActivity extends Activity implements OnValueChangeListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     if (((iTIVAapp)getApplication()).modelo2.equals("halogenado")==false){ 
     setContentView(R.layout.objetivospage_sep); 
     } 
     else  
     { 
     setContentView(R.layout.objetivospage_bal); 
     }     

     //valores por defecto 
     ((iTIVAapp)getApplication()).Tinduccion=5;  
     ((iTIVAapp)getApplication()).tcx=180; 
     ((iTIVAapp)getApplication()).objetivo="efecto"; 
     //((iTIVAapp)getApplication()).deltainfusion=15; 
     ((iTIVAapp)getApplication()).cambio=true; 

     final Button btn_opt = (Button)findViewById(R.id.button_optimizar); 

     if (((iTIVAapp)getApplication()).mezclado==true){ 
      btn_opt.setVisibility(View.VISIBLE); 

     } else if (((iTIVAapp)getApplication()).modelo2.equals("halogenado")==false) { 
      btn_opt.setVisibility(View.GONE); 
     } 

     ///////// 
     final Button btn_ind = (Button)findViewById(R.id.button_induccion); 
     btn_ind.setText((int)((iTIVAapp)getApplication()).Tinduccion+" "+getString(R.string._min)); 
     //se crean los objetivos del farmaco 1 

     final NumberPicker np_ini1 = (NumberPicker) findViewById(R.id.numberPicker_ini1); 
     final NumberPicker np_man1 = (NumberPicker) findViewById(R.id.numberPicker_man1); 
     final NumberPicker np_sal1 = (NumberPicker) findViewById(R.id.numberPicker_sal1); 

     String[] nums = new String[120]; 

     for(int i=1; i<nums.length+1; i++) 
      nums[i-1] = Double.toString(i/10.0); 

     np_ini1.setMaxValue(nums.length-1); 
     np_ini1.setMinValue(0); 
     np_ini1.setDisplayedValues(nums); 
     np_ini1.setValue((int) (((iTIVAapp)getApplication()).ini1*10-1)); 
     np_ini1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_ini1.setOnValueChangedListener(this); 

     np_man1.setMaxValue(nums.length-1); 
     np_man1.setMinValue(0); 
     np_man1.setDisplayedValues(nums); 
     np_man1.setValue((int) (((iTIVAapp)getApplication()).man1*10-1)); 
     np_man1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_man1.setOnValueChangedListener(this); 

     np_sal1.setMaxValue(nums.length-1); 
     np_sal1.setMinValue(0); 
     np_sal1.setDisplayedValues(nums); 
     np_sal1.setValue((int) (((iTIVAapp)getApplication()).sal1*10-1)); 
     np_sal1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_sal1.setOnValueChangedListener(this); 

     if (((iTIVAapp)getApplication()).modelo2.equals("schnider") && ((iTIVAapp)getApplication()).mezclado==false){ 
     //se crean los objetivos del farmaco 2 

      final NumberPicker np_ini2 = (NumberPicker) findViewById(R.id.numberPicker_ini2); 
      final NumberPicker np_man2 = (NumberPicker) findViewById(R.id.numberPicker_man2); 
      final NumberPicker np_sal2 = (NumberPicker) findViewById(R.id.numberPicker_sal2); 

     String[] nums2 = new String[80]; 

     for(int i=1; i<nums2.length+1; i++) 
      nums2[i-1] = Double.toString(i/10.0); 

     np_ini2.setMaxValue(nums2.length-1); 
     np_ini2.setMinValue(0); 
     np_ini2.setDisplayedValues(nums2); 
     np_ini2.setValue((int) (((iTIVAapp)getApplication()).ini2*10-1)); 
     np_ini2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_ini2.setOnValueChangedListener(this); 

     np_man2.setMaxValue(nums2.length-1); 
     np_man2.setMinValue(0); 
     np_man2.setDisplayedValues(nums2); 
     np_man2.setValue((int) (((iTIVAapp)getApplication()).man2*10-1)); 
     np_man2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_man2.setOnValueChangedListener(this); 

     np_sal2.setMaxValue(nums2.length-1); 
     np_sal2.setMinValue(0); 
     np_sal2.setDisplayedValues(nums2); 
     np_sal2.setValue((int) (((iTIVAapp)getApplication()).sal2*10-1)); 
     np_sal2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
     np_sal2.setOnValueChangedListener(this); 
     } 
     else if (((iTIVAapp)getApplication()).mezclado==true){ 
      //se crean los objetivos del farmaco 2 

      final NumberPicker np_ini2 = (NumberPicker) findViewById(R.id.numberPicker_ini2); 
      final NumberPicker np_man2 = (NumberPicker) findViewById(R.id.numberPicker_man2); 
      final NumberPicker np_sal2 = (NumberPicker) findViewById(R.id.numberPicker_sal2); 

      String[] nums2 = new String[81]; 

      for(int i=1; i<nums2.length; i++) 
       nums2[i-1] = Double.toString(i/10.0); 

      nums2[80] = "-"; 

      np_ini2.setMaxValue(nums2.length-1); 
      np_ini2.setMinValue(0); 
      np_ini2.setDisplayedValues(nums2); 
      np_ini2.setValue(80); 
      np_ini2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
      np_ini2.setOnValueChangedListener(this); 

      np_man2.setMaxValue(nums2.length-1); 
      np_man2.setMinValue(0); 
      np_man2.setDisplayedValues(nums2); 
      np_man2.setValue(80); 
      np_man2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
      np_man2.setOnValueChangedListener(this); 

      np_sal2.setMaxValue(nums2.length-1); 
      np_sal2.setMinValue(0); 
      np_sal2.setDisplayedValues(nums2); 
      np_sal2.setValue(80); 
      np_sal2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
      np_sal2.setOnValueChangedListener(this);  
     } 
     else if (((iTIVAapp)getApplication()).modelo2.equals("halogenado")){ 
      NumberPicker np_mac = (NumberPicker) findViewById(R.id.numberPicker_mac); 
      String[] nums2 = new String[30]; 

      for(int i=1; i<nums2.length+1; i++) 
       nums2[i-1] = Double.toString(i/10.0); 

      np_mac.setMaxValue(nums2.length-1); 
      np_mac.setMinValue(0); 
      np_mac.setDisplayedValues(nums2); 
      np_mac.setValue(0); 
      np_mac.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
      np_mac.setOnValueChangedListener(this); 
     } 


    } 



    @Override 
    protected void onResume() { 
      super.onResume(); 

      NumberPicker np_ini1 = (NumberPicker) findViewById(R.id.numberPicker_ini1); 
      NumberPicker np_man1 = (NumberPicker) findViewById(R.id.numberPicker_man1); 
      NumberPicker np_sal1 = (NumberPicker) findViewById(R.id.numberPicker_sal1); 


      np_ini1.setValue((int) (((iTIVAapp)getApplication()).ini1*10-1)); 
      np_man1.setValue((int) (((iTIVAapp)getApplication()).man1*10-1)); 
      np_sal1.setValue((int) (((iTIVAapp)getApplication()).sal1*10-1)); 


      if (((iTIVAapp)getApplication()).modelo2.equals("halogenado")==false){ 
       NumberPicker np_ini2 = (NumberPicker) findViewById(R.id.numberPicker_ini2); 
       NumberPicker np_man2 = (NumberPicker) findViewById(R.id.numberPicker_man2); 
       NumberPicker np_sal2 = (NumberPicker) findViewById(R.id.numberPicker_sal2); 

       np_ini2.setValue((int) (((iTIVAapp)getApplication()).ini2*10-1)); 
       np_man2.setValue((int) (((iTIVAapp)getApplication()).man2*10-1)); 
       np_sal2.setValue((int) (((iTIVAapp)getApplication()).sal2*10-1)); 

       if (((iTIVAapp)getApplication()).mezclado==true){ 

        if (((iTIVAapp)getApplication()).ini2==0 && ((iTIVAapp)getApplication()).man2==0){ 
         np_ini2.setValue(80); 
         np_man2.setValue(80); 
        } else { 
        np_ini2.setValue((int) (((iTIVAapp)getApplication()).ini2*10-1)); 
        np_man2.setValue((int) (((iTIVAapp)getApplication()).man2*10-1)); 
        } 

        np_sal2.setValue(80); 
       } 

      } 

    } 

..... 

回答

1
Caused by: java.lang.NullPointerException 
    at com.raigo.itivamin.ObjetivosActivity.onCreate(ObjetivosActivity.java:25) 

的錯誤似乎指向這一行:

if (((iTIVAapp)getApplication()).modelo2.equals("halogenado")==false){ 

因此,無論(iTIVAapp)getApplication()爲空或.modelo2爲空。您可以使用調試器或通過LogCat語句測試哪個值爲空。

如果modelo2可能會或可能不會爲空,你可以向後寫你的條件:

if(!"halogenado".equals(((iTIVAapp)getApplication()).modelo2)){ 
    // "!" means "!= true" which must mean "false" for a boolean 
+0

是什麼原因造成的應用iTIVAapp類後的某個時間重新設置?因爲如果我5分鐘後返回我的應用,是正確的,我需要保存應用程序類中的數據,以便當我的應用程序進入後臺時,當用戶返回應用程序時,可以返回iTIVA應用程序類中變量的所有值。 (原諒我的英語不是我的天真語言) –

+0

您需要保存重要數據。我建議使用SharedPreferences將這些信息保存在永久位置,請閱讀['Data Storage'](https://developer.android.com/guide/topics/data/data-storage.html)以獲取示例和其他方法。 – Sam