Autore Topic: NullPointerException durante la creazione dei widgets  (Letto 1108 volte)

Offline xmndcstl

  • Nuovo arrivato
  • *
  • Post: 6
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Xiaomi mi4
  • Sistema operativo:
    Windows 8.1 & 7
NullPointerException durante la creazione dei widgets
« il: 22 Maggio 2015, 20:18:24 CEST »
0
Salve ragazzi, mi serve aiuto in una applicazione di matematica finanziaria.
Al momento del passaggio dalla splash activity iniziale alla activity vera e propria dell'app l'applicazione si blocca e va in crash.
Questo è l'errore
Codice: [Seleziona]
05-22 19:44:35.110  19763-19763/com.xmndcstl.matfinanziaria E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xmndcstl.matfinanziaria/com.xmndcstl.matfinanziaria.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2018)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2119)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4952)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:2008)
            at com.xmndcstl.matfinanziaria.MainActivity.<init>(MainActivity.java:25)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1056)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2009)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2119)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4952)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
            at dalvik.system.NativeStart.main(Native Method)

L'errore fa riferimento alla MainActivity alla riga 25

Codice (Java): [Seleziona]
public class MainActivity extends ActionBarActivity {
    public double capitale = 0;
    public double t_anni = 0;
    public double t_mesi = 0;
    public double t_giorni = 0;
    public double tasso = 0;
    public double montante =0;
    public double tempoTot = 0;
    calcolatore calcola = new calcolatore();

    final EditText tb_capitale = (EditText) findViewById(R.id.tbCapitale);
    final EditText tb_anni = (EditText) findViewById(R.id.tbAnni);
    final EditText tb_mesi = (EditText) findViewById(R.id.tbMesi);
    final EditText tb_giorni = (EditText) findViewById(R.id.tbGiorni);
    final EditText tb_tasso = (EditText) findViewById(R.id.tbTasso);
    final RadioButton radX = (RadioButton) findViewById(R.id.radioX);
    final EditText tb_montante =(EditText) findViewById(R.id.tbMontante);
    final Button btnAvvio = (Button) findViewById(R.id.btnCalcola);
    final TextView output = (TextView) findViewById(R.id.tbOutput);

La riga 25 è "final EditText tb_capitale = (EditText) findViewById(R.id.tbCapitale);" Ma l'errore avviene in ogni singola dichiarazione di una nuova istanza; più precisamente l'errore è una NullPointerException quindi ho pensato che l'argomento di FindViewById fosse null, così sono andato nella classe r per vedere se il codice generato fosse corretto ed infatti è cosi, nella classe id vengono generati tutti gli elementi di cui ho bisogno

Codice (Java): [Seleziona]
public static final class id {
        public static final int action_bar=0x7f080031;
        public static final int action_bar_activity_content=0x7f080000;
        public static final int action_bar_container=0x7f080030;
        public static final int action_bar_root=0x7f08002c;
        public static final int action_bar_spinner=0x7f080001;
        public static final int action_bar_subtitle=0x7f08001f;
        public static final int action_bar_title=0x7f08001e;
        public static final int action_context_bar=0x7f080032;
        public static final int action_menu_divider=0x7f080002;
        public static final int action_menu_presenter=0x7f080003;
        public static final int action_mode_bar=0x7f08002e;
        public static final int action_mode_bar_stub=0x7f08002d;
        public static final int action_mode_close_button=0x7f080020;
        public static final int action_settings=0x7f08004f;
        public static final int activity_chooser_view_content=0x7f080021;
        public static final int always=0x7f080016;
        public static final int beginning=0x7f080013;
        public static final int btnCalcola=0x7f08004b;
        public static final int checkbox=0x7f080029;
        public static final int collapseActionView=0x7f080017;
        public static final int decor_content_parent=0x7f08002f;
        public static final int default_activity_button=0x7f080024;
        public static final int dialog=0x7f08001b;
        public static final int disableHome=0x7f08000c;
        public static final int dropdown=0x7f08001c;
        public static final int edit_query=0x7f080033;
        public static final int end=0x7f080014;
        public static final int expand_activities_button=0x7f080022;
        public static final int expanded_menu=0x7f080028;
        public static final int home=0x7f080004;
        public static final int homeAsUp=0x7f08000d;
        public static final int icon=0x7f080026;
        public static final int ifRoom=0x7f080018;
        public static final int image=0x7f080023;
        public static final int image_logo=0x7f08004e;
        public static final int layoutXsplash=0x7f08004d;
        public static final int lbCapitale=0x7f080040;
        public static final int lbMontante=0x7f080047;
        public static final int lbTasso=0x7f080049;
        public static final int lbTempo=0x7f080042;
        public static final int lbTitolo=0x7f08003f;
        public static final int listMode=0x7f080009;
        public static final int list_item=0x7f080025;
        public static final int middle=0x7f080015;
        public static final int never=0x7f080019;
        public static final int none=0x7f08000e;
        public static final int normal=0x7f08000a;
        public static final int progress_circular=0x7f080005;
        public static final int progress_horizontal=0x7f080006;
        public static final int radio=0x7f08002b;
        public static final int radioX=0x7f080043;
        public static final int search_badge=0x7f080035;
        public static final int search_bar=0x7f080034;
        public static final int search_button=0x7f080036;
        public static final int search_close_btn=0x7f08003b;
        public static final int search_edit_frame=0x7f080037;
        public static final int search_go_btn=0x7f08003d;
        public static final int search_mag_icon=0x7f080038;
        public static final int search_plate=0x7f080039;
        public static final int search_src_text=0x7f08003a;
        public static final int search_voice_btn=0x7f08003e;
        public static final int shortcut=0x7f08002a;
        public static final int showCustom=0x7f08000f;
        public static final int showHome=0x7f080010;
        public static final int showTitle=0x7f080011;
        public static final int split_action_bar=0x7f080007;
        public static final int submit_area=0x7f08003c;
        public static final int tabMode=0x7f08000b;
        public static final int tbAnni=0x7f080044;
        public static final int tbCapitale=0x7f080041;
        public static final int tbGiorni=0x7f080046;
        public static final int tbMesi=0x7f080045;
        public static final int tbMontante=0x7f080048;
        public static final int tbOutput=0x7f08004c;
        public static final int tbTasso=0x7f08004a;
        public static final int title=0x7f080027;
        public static final int up=0x7f080008;
        public static final int useLogo=0x7f080012;
        public static final int withText=0x7f08001a;
        public static final int wrap_content=0x7f08001d;
    }
Non capisco dove sbaglio e sopratutto perchè l'activity di splash funzioni e la main no.
Spero vivamente qualcuno riesca ad aiutarmi, grazie mille!

Offline LinkOut

  • Utente normale
  • ***
  • Post: 269
  • Respect: +37
    • Mostra profilo
  • Dispositivo Android:
    Xiaomi Mi5
Re:NullPointerException durante la creazione dei widgets
« Risposta #1 il: 23 Maggio 2015, 10:05:19 CEST »
0
Codice (Java): [Seleziona]
  final EditText tb_capitale = (EditText) findViewById(R.id.tbCapitale);
    final EditText tb_anni = (EditText) findViewById(R.id.tbAnni);
    final EditText tb_mesi = (EditText) findViewById(R.id.tbMesi);
    final EditText tb_giorni = (EditText) findViewById(R.id.tbGiorni);
    final EditText tb_tasso = (EditText) findViewById(R.id.tbTasso);
    final RadioButton radX = (RadioButton) findViewById(R.id.radioX);
    final EditText tb_montante =(EditText) findViewById(R.id.tbMontante);
    final Button btnAvvio = (Button) findViewById(R.id.btnCalcola);
    final TextView output = (TextView) findViewById(R.id.tbOutput);

Dovrebbe essere nel metodo onCreate() dell'Activity , sotto setContentView ();

Saluti.

PS:
Edit: ActionBarActivity è stato deprecato, ora c'è AppCompatActivity
« Ultima modifica: 23 Maggio 2015, 10:08:55 CEST da LinkOut »