NumberFormatException:无效的int:""

时间:2017-04-27 06:47:29

标签: java android numberformatexception

错误:

  

04-27 12:16:12.799 5339-5339 / com.agilerise.preschool E / UncaughtException:java.lang.NumberFormatException:无效的int:""                                                                                 at> java.lang.Integer.invalidInt(Integer.java:138)                                                                                 at> java.lang.Integer.parseInt(Integer.java:358)                                                                                 at> java.lang.Integer.parseInt(Integer.java:334)                                                                                 在java.lang.Integer.valueOf(Integer.java:525)                                                                                 at> com.agilerise.preschool.activity.CircularFragment.display(CircularFragment.java:230)                                                                                 at> com.agilerise.preschool.activity.CircularFragment.onViewCreated(CircularFragment.java:189)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)                                                                                 在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)                                                                                 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                                 在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517)                                                                                 在android.os.Handler.handleCallback(Handler.java:815)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:104)                                                                                 在android.os.Looper.loop(Looper.java:194)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5576)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在java.lang.reflect.Method.invoke(Method.java:372)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:955)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)   04-27 12:16:13.050 5339-5339 / com.agilerise.preschool E / AndroidRuntime:致命异常:主要                                                                          过程:com.agilerise.preschool,PID:5339                                                                          java.lang.NumberFormatException:无效的int:""                                                                              在java.lang.Integer.invalidInt(Integer.java:138)                                                                              在java.lang.Integer.parseInt(Integer.java:358)                                                                              在java.lang.Integer.parseInt(Integer.java:334)                                                                              在java.lang.Integer.valueOf(Integer.java:525)                                                                              在com.agilerise.preschool.activity.CircularFragment.display(CircularFragment.java:230)                                                                              在com.agilerise.preschool.activity.CircularFragment.onViewCreated(CircularFragment.java:189)                                                                              在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)                                                                              在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)                                                                              在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)                                                                              在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                              在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517)                                                                              在android.os.Handler.handleCallback(Handler.java:815)                                                                              在android.os.Handler.dispatchMessage(Handler.java:104)                                                                              在android.os.Looper.loop(Looper.java:194)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5576)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在java.lang.reflect.Method.invoke(Method.java:372)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:955)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)   04-27 12:16:13.336 5339-6227 / com.agilerise.preschool E / NativeCrypto:ssl = 0xb0764400 cert_verify_callback x509_store_ctx = 0x9abfb428 arg = 0x0   04-27 12:16:13.336 5339-6227 / com.agilerise.preschool E / NativeCrypto:ssl = 0xb0764400 cert_verify_callback调用verifyCertificateChain authMethod = ECDHE_RSA

代码:

public class  CircularFragment extends Fragment implements AsyncResponse {
    SessionManager session;
    String un, type, fulldate, formattedDate, adate, tablename, pmeter, time,acdyear,timestamp;
    Button add, sub;
    TextView display, datanotavailable;
    SimpleDateFormat df, fulmonth;
    DatabaseHandler db;
    RecyclerView mRecyclerView;
    CircularAdapter mAdapter;
    RelativeLayout relativeLayout;
    FoodmenuAdapter fAdapter;
    JSONParserforMap jsonParser = new JSONParserforMap();
    RecyclerView.LayoutManager mLayoutManager;
    private static final String TAG_SID = "aid";
    private static final String TAG_SDATE = "adate";
    private static final String TAG_SCONTENT = "acontent";
    private static final String TAG_TIME = "time";
    private static final String TAG_SDATETIME = "adate_time";
    private SQLiteDatabase dataBase;
    public static int color;
    int pos;
    GetdataAsyncTask    temp;
    String timestamp1,timestamp2,month1,classid;
    ArrayList<HashMap<String, String>> arraylist;
    Date mont;
    Date date1;
    ProgressDialog pd;
    public CircularFragment() {
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        session = new SessionManager(getContext());
        HashMap<String, String> user = session.getUserDetails();
        un = user.get(SessionManager.KEY_NAME);
        type = user.get(SessionManager.KEY_TYPE);
        Bundle bundle = this.getArguments();
        pos = bundle.getInt("pos", 0);
        color = bundle.getInt("color",R.color.colorPrimary);
        timestamp1=bundle.getString("time", "");
        if(!timestamp1.equals("")){
            timestamp2= timestamp1.substring(0, 7);
            month1=timestamp1.substring(5, 6);

            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
            try {
                date1 = simpleDateFormat.parse(timestamp2);
                Log.e("timetable",timestamp2);
            } catch (ParseException e) {
                e.printStackTrace();
            }}
        switch (pos) {
            case 1:
                tablename = DatabaseHandler.TABLE_CIRCULAR;
                pmeter = "circular";
                break;
            case 2:
                tablename = DatabaseHandler.TABLE_CIRCULAR;
                pmeter = "holiday";
                break;
            case 3:
                tablename = DatabaseHandler.TABLE_EVENTS;
                pmeter = "events";
                break;
            case 4:
                tablename = DatabaseHandler.TABLE_EVENTS;
                pmeter = "foodmenu";
                break;
        }
        pd = new ProgressDialog(getContext());
        pd.setMessage("Please wait...");
        pd.setCanceledOnTouchOutside(true);
        db = new DatabaseHandler(getContext());
        if (pos != 3 || pos != 4) {
            time = db.SingleCirculargettimestamp(pmeter, type, un);
        } else {
            time = db.SingleEventsgettimestamp(pmeter, type, un);
        }
        boolean internet = new Internetcheck(getContext()).isOnline();
        if (internet){
            temp= (GetdataAsyncTask) new GetdataAsyncTask(this, db, pmeter, type, un, time).execute();
        }}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_circular, container, false);
        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        datanotavailable=(TextView)getView().findViewById(R.id.datanotavailable);
        add = (Button) getView().findViewById(R.id.beAdd);
        relativeLayout=(RelativeLayout)getView().findViewById(R.id.qqty);
        sub = (Button) getView().findViewById(R.id.beSub);
        display = (TextView) getView().findViewById(R.id.tveDisplay);
        relativeLayout.setBackgroundColor(color);
        final Calendar c = Calendar.getInstance();
        SharedPreferences sharedpreferences12 = getActivity().getSharedPreferences("classvalidation", Context.MODE_PRIVATE);

        classid= sharedpreferences12.getString("classid", "");
        System.out.println("Current time => " + c.getTime());
        df = new SimpleDateFormat("yyyy-MM");
        if(timestamp1.equals("")){
            formattedDate = df.format(c.getTime());
            fulmonth = new SimpleDateFormat("MMM-yyyy");
            fulldate = fulmonth.format(c.getTime());
        }else {
            formattedDate=df.format(date1);


            fulmonth = new SimpleDateFormat("MMM-yyyy");
           /* try {
            *//*   mont= fulmonth.parse(date1);*//*
            } catch (ParseException e) {
                e.printStackTrace();
            }*/

            fulldate = fulmonth.format(date1);
        }



        display.setText(fulldate);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int m= Calendar.MONTH;
                Date s=  c.getTime();
                c.add(Calendar.MONTH, 1);
                formattedDate = df.format(c.getTime());
                fulldate = fulmonth.format(c.getTime());
                display.setText(fulldate);
                display();
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                c.add(Calendar.MONTH, -1);
                formattedDate = df.format(c.getTime());
                fulldate = fulmonth.format(c.getTime());
                display.setText(fulldate);
                display();
            }
        });
        display();

        if(pd!=null){
            if(temp!=null){
                if(temp.getStatus() == AsyncTask.Status.PENDING){
                    pd.show();
                }

                if(temp.getStatus() == AsyncTask.Status.RUNNING){
                    // My AsyncTask is currently doing work in doInBackground()
                    pd.show();
                }
                final Timer myTimer = new Timer();
                int i=10000;

                myTimer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        if(temp.getStatus() == AsyncTask.Status.FINISHED){
                            // My AsyncTask is done and onPostExecute was called
                            pd.dismiss();
                            myTimer.cancel();}
                    }
                }, 2000,i);









            }}
    }


    public void display() {
        SharedPreferences sharedPreferencess1 = getActivity().getSharedPreferences("acadamicyear", Context.MODE_PRIVATE);
        acdyear= sharedPreferencess1.getString("acamadic", "");
        Integer result = Integer.valueOf(acdyear);

        dataBase = db.getReadableDatabase();
        //Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + tablename + " WHERE type = '" + type + "' and " + DatabaseHandler.KEY_FUNIQ + " = '" + pmeter + "' and " + DatabaseHandler.KEY_USERNAME + " = '" + un + "' and strftime('%Y-%m', date) = '" + formattedDate + "' ORDER BY " + DatabaseHandler.KEY_DATE + " ASC", null);
        Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + tablename + " WHERE type = '" + type + "' and " + DatabaseHandler.KEY_FUNIQ + " = '" + pmeter +"' and ( "+DatabaseHandler.KEY_CLASS_ID+" = '"+classid +"' OR "+DatabaseHandler.KEY_CLASS_ID+" = 0 ) and " + DatabaseHandler.KEY_USERNAME + " = '" + un + "' and strftime('%Y-%m', date) = '" + formattedDate + "' ORDER BY " + DatabaseHandler.KEY_TIMESTAMP + " DESC", null);
        arraylist = new ArrayList<HashMap<String, String>>();
        arraylist.clear();
        try {
            if (mCursor.moveToFirst()) {
                Integer result1;
               // do {
                    HashMap<String, String> data1 = new HashMap<String, String>();
                    timestamp = mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_TIMESTAMP));
                    String[] parts = timestamp.split("-");
                    String tmstpyear = parts[0];
                    result1 = Integer.valueOf(tmstpyear);
               // } while (mCursor.moveToNext());


                if (result1 >= result) {
                    if (mCursor.moveToFirst()) {
                        do {
                            HashMap<String, String> data = new HashMap<String, String>();
                            data.put(TAG_SID, mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_ID)));
                            try {
                                adate = mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_DATE));
                                SimpleDateFormat beforegetting = new SimpleDateFormat("yyyy-MM-dd");
                                SimpleDateFormat aftergetting = new SimpleDateFormat("d MMM, yyyy");
                                Date dates = beforegetting.parse(adate);
                                adate = aftergetting.format(dates);
                            } catch (Exception e) {
                                Log.e("TAG", "date" + e);
                            }
                            if (pos == 3)
                                data.put(TAG_SDATE, adate + ", " + mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_TIME)));
                            else
                                data.put(TAG_SDATE, adate);
                            if (pos == 4) {
                                data.put(TAG_TIME, mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_TIME)));
                            }
                            data.put(TAG_SCONTENT, mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_MESSAGE)));
                            data.put(TAG_SDATETIME, mCursor.getString(mCursor.getColumnIndex(DatabaseHandler.KEY_TIMESTAMP)));
                            arraylist.add(data);
                        } while (mCursor.moveToNext());
                    }
                }
                if (arraylist.isEmpty())
                    datanotavailable.setVisibility(View.VISIBLE);
                else
                    datanotavailable.setVisibility(View.GONE);
                mRecyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view);
                mRecyclerView.setHasFixedSize(true);
                mLayoutManager = new LinearLayoutManager(getActivity());
                mRecyclerView.setLayoutManager(mLayoutManager);
                if (pos != 4) {
                    mAdapter = new CircularAdapter(getContext(), arraylist);
                    mRecyclerView.setAdapter(mAdapter);
                } else {
                    fAdapter = new FoodmenuAdapter(getContext(), arraylist);
                    mRecyclerView.setAdapter(fAdapter);
                }
            }

        }catch (Exception e){

        }}

    public void processFinish(String output) {
        if (output.equals("1")) {
            try {
                display();
            } catch (Exception e) {

            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

此异常在 LineNo的CircularFragment.java处发生。在display()方法中 230 ,您可以在其中调用: Integer.parseInt(yourString), 你应该在那里申请以下检查:

 if(yourString.equals("")){
       yourString = "0";
    }
    //code of Integer.parseInt(yourString)

答案 1 :(得分:0)

您正在尝试将空字符串转换为整数。在将输入转换为整数格式之前检查输入是否为空。

答案 2 :(得分:0)

In display method

    acdyear= sharedPreferencess1.getString("acamadic","0");