由于未知原因,应用程序启动时出现nullpointer异常

时间:2012-07-01 14:33:01

标签: android exception nullpointerexception

07-01 17:47:23.517: E/AndroidRuntime(932): FATAL EXCEPTION: main
07-01 17:47:23.517: E/AndroidRuntime(932): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.ActivityThread.access$1300(ActivityThread.java:123)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.os.Looper.loop(Looper.java:137)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-01 17:47:23.517: E/AndroidRuntime(932):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 17:47:23.517: E/AndroidRuntime(932):  at java.lang.reflect.Method.invoke(Method.java:511)
07-01 17:47:23.517: E/AndroidRuntime(932):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-01 17:47:23.517: E/AndroidRuntime(932):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-01 17:47:23.517: E/AndroidRuntime(932):  at dalvik.system.NativeStart.main(Native Method)
07-01 17:47:23.517: E/AndroidRuntime(932): Caused by: java.lang.NullPointerException
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
07-01 17:47:23.517: E/AndroidRuntime(932):  at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
07-01 17:47:23.517: E/AndroidRuntime(932):  ... 11 more

当我启动我的应用程序时出现上述错误。此错误不会始终发生,但只会偶尔发生一次(每20-30次启动一次)并且永远不会连续两次发生

有人能告诉我这个错误产生的代码(xml或java)是什么以及如何解决它? 这是我的代码:

public class ProjectBunkitActivity extends Activity {
    Integer z=0,k,i=0;
    static String s1;
    static String s2;
    static String s3;
    static int cpd=0;
    int day,month,year;

    String [] p1,p2,p3,p4,p5,p6;
    String so[]=new String[12];
    String subj1[]=new String[20];

    static String subj2[]=null;
    String s,sn;

    Button b1,b2,b3;
    Button add,sub;
    Button btn;
    EditText t1,t2,t3;
    Exception x;
    LinearLayout linearlayout;
    DatePicker datePicker;
    Date picdate;

    static int pos=0;


    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        SharedPreferences prefsfile = getSharedPreferences("myfile",0); 
        linearlayout=(LinearLayout)findViewById(R.id.leny);
        t3=(EditText)findViewById(R.id.cls2);

        if( prefsfile.getInt("cpd",0)!=0 && prefsfile.getInt("nos",0)!=0 && Timetablledisplay.flag==0 ){

            Intent i=new Intent(ProjectBunkitActivity.this,Timetablledisplay.class);
            ProjectBunkitActivity.cpd=prefsfile.getInt("cpd", 0);
            ProjectBunkitActivity.pos=prefsfile.getInt("nos", 0);
            startActivity(i);

            finish();

        }
        else{

            Timetablledisplay.flag=1;

        }


        b1=(Button)findViewById(R.id.btn);


        datePicker = (DatePicker) findViewById(R.id.datePicker1);

        t1=(EditText)findViewById(R.id.tdys);
        t2=(EditText)findViewById(R.id.cls1);



        add=(Button)findViewById(R.id.sub1);
        sub=(Button)findViewById(R.id.add);


        datepicupdate();

        b1.setOnClickListener(new OnClickListener() {

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


                    s1=t1.getText().toString();
                    s2=t2.getText().toString();
                    s3=t3.getText().toString();
                if(s1.compareTo("")!=0 && s2.compareTo("")!=0 && linearlayout.getChildCount()!=0){

                    s1=t1.getText().toString();
                    s2=t2.getText().toString();
                    s3=t3.getText().toString();
                    cpd=Integer.parseInt(s2);


                    int l=0;
                    pos=linearlayout.getChildCount(); 
                    String subj[]=new String[pos];
                    try{

                        for(int j=0;j<pos;j++) 
                        {   

                            EditText edt1=(EditText)findViewById(j);
                            sn=edt1.getText().toString();
                            subj[l++]=sn;

                        }


                    }
                    catch(Exception e)
                    {


                    }  

                    subj2=subj;

                    Toast toast=Toast.makeText(ProjectBunkitActivity.this,subj2[0],Toast.LENGTH_SHORT);
                    toast.show();

                    Intent i=new Intent(ProjectBunkitActivity.this,TimetableEdit.class);
                    i.putExtra("course",s1);
                    i.putExtra("classes", s2);
                    i.putExtra("days", s3);
                    i.putExtra("subject",subj);
                    startActivity(i);

                }else {

                    Toast toast=Toast.makeText(ProjectBunkitActivity.this,"Please fill all field before proceeding",Toast.LENGTH_SHORT);
                    toast.show();

                }

            }
        });








        add.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                try{
                    pos=linearlayout.getChildCount(); 
                    EditText edt=(EditText) new EditText(getApplicationContext());
                    edt.setId(pos);
                    pos++;
                    s=String.valueOf(pos);
                    edt.setHint("Enter the subject name");
                    linearlayout.addView(edt);


                }
                catch(Exception e)
                {

                }
            }
        });   


        sub.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{

                    pos=linearlayout.getChildCount(); 
                    s=String.valueOf(pos);
                    linearlayout.removeViewAt(pos-1);
                    pos--;


                }catch(Exception n){


                }
            }  

        });





    }







    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

    }







    private void datepicupdate() {
        // TODO Auto-generated method stub
        try{

            if(year!=0){

                datePicker.updateDate(year, month, day);


            }

        }catch (Exception e) {
            // TODO: handle exception

        }

    }








    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        try{
            int jaz=Integer.parseInt(t2.getText().toString());

            day = datePicker.getDayOfMonth();
            month = datePicker.getMonth();
            year = datePicker.getYear();


            SharedPreferences.Editor editor = getSharedPreferences("myfile",0).edit();
            editor.putString("text", t1.getText().toString());
            editor.putInt("cpd", jaz);
            editor.putInt("nos", linearlayout.getChildCount());
            editor.putInt("day",day);
            editor.putInt("month",month);
            editor.putInt("year",year);

            if(pos==0)
            {

                pos=linearlayout.getChildCount();

            }

            editor.putInt("pos",pos);   


            int l=0;
            try{

                for(int j=0;j<=pos-1;j++) 
                {   

                    EditText edt1=(EditText)findViewById(j);
                    sn=edt1.getText().toString();
                    editor.putString("sub0"+l,sn);
                    l++;

                }
            }
            catch(Exception e)
            {

            } 

            editor.commit();
        }catch (Exception e) {
            // TODO: handle exception
        }

    }




    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

    }




    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        try{

            SharedPreferences prefs = getSharedPreferences("myfile",0); 
            String restoredText = prefs.getString("text", null); 

            String[] sw=new String[prefs.getInt("cpd",0)];

            cpd = prefs.getInt("cpd", 0);
            int restoredText2= prefs.getInt("nos", 0);
            int pos1=prefs.getInt("pos",2);
            int l=0;
            if (restoredText != null) {
                t1.setText(restoredText, EditText.BufferType.EDITABLE);
                t2.setText(""+cpd, EditText.BufferType.EDITABLE);
                t3.setText(""+restoredText2, EditText.BufferType.EDITABLE);
                day= prefs.getInt("day", 1);
                month= prefs.getInt("month", 1);
                year= prefs.getInt("year",2012);

                datepicupdate();



                linearlayout.removeAllViews();
                for(int j=0;j<=pos1-1;j++) 
                {    

                    sw[l]=prefs.getString("sub0"+l,null); 

                    EditText edt1=(EditText) new EditText(getApplicationContext());
                    edt1.setId(j);
                    edt1.setHint("Enter the subject name");
                    edt1.setText(sw[l]);
                    linearlayout.addView(edt1);
                    l++;

                } 



            }

        }catch (Exception e) {
            // TODO: handle exception

        }
    }




    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();


    }




}

0 个答案:

没有答案