Android:使用StringTokenizer的NoSuchElementException

时间:2015-07-26 08:29:54

标签: android stringtokenizer

这是我的Android项目的一个类。我得到NoSuchElementException

当我将StringTokenizer stst1以及st.nextToken()st1.nextToken()注释掉时,应用运行正常。

package com.example.client_nic;

import java.util.Calendar;
import java.util.StringTokenizer;

import com.example.client_nic.DataFields.fields;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Meeting extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.clientmeeting, container,false);  //why false still unclear
        EditText nameev= (EditText)view.findViewById(R.id.nameev);
        final String name = nameev.getText().toString();
        if(name==null){
            Toast.makeText(getActivity(), "Name cannot be Empty", Toast.LENGTH_SHORT).show();
        }

        EditText contactev= (EditText)view.findViewById(R.id.contactev);
        final String contact = contactev.getText().toString();

        EditText addressev= (EditText)view.findViewById(R.id.Addressev);
        final String address = addressev.getText().toString();

        EditText remarksev= (EditText)view.findViewById(R.id.remarksev);
        final String remarks = remarksev.getText().toString();

        EditText dateev = (EditText)view.findViewById(R.id.dateev);
        final String date = dateev.getText().toString();
        StringTokenizer st = new StringTokenizer(date, "/");


        final int day = Integer.parseInt(st.nextToken());
        final int month = Integer.parseInt(st.nextToken());
        final int year = Integer.parseInt(st.nextToken());

        EditText timeev = (EditText)view.findViewById(R.id.timeev);
        final String time = timeev.getText().toString();

        StringTokenizer st1 = new StringTokenizer(time, ":");
        final int hourOfDay  = Integer.parseInt(st1.nextToken());
        final int minute = Integer.parseInt(st1.nextToken());

        Button button = (Button)view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

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

                Calendar calender = Calendar.getInstance();
                calender.set(year, month, day, hourOfDay, minute);
                long inputtime = calender.getTimeInMillis();


                long presenttime = System.currentTimeMillis();
                if(inputtime<presenttime){
                    Toast.makeText(getActivity(), "Time has already passed", Toast.LENGTH_SHORT).show();
                }
                else{
                    String insert = "INSERT INTO "+fields.table_name+" VALUES('"+name+"','"+contact+"','"+address+"','"+remarks+"','"+date+"','"+time+"' ) ";                   
                    Datastore ds = new Datastore(getActivity());
                    SQLiteDatabase sql = ds.getWritableDatabase();
                    sql.execSQL(insert);
                }
            }
        });

        return view;
    }
}

2 个答案:

答案 0 :(得分:1)

您正在使用st1.nextToken() st.nextToken(),而没有产生任何令牌

  

NoSuchElementException异常

你只需检查hasMoreTokens是否为真,然后就可以使用它了:

while(token.hasMoreTokens())
{
//use st.nextToken()
}

Documentation including an example on how to use StringTokenizer!

答案 1 :(得分:0)

这意味着您在EditText dateev中输入的日期不包含足够/个字符或您在EditText timeev中输入的时间不包含足够的:个字符。

而不是StringTokenizer您应该按照StringTokenizer的班级概述中的建议使用String.split()。这当然本身不会解决您的问题。

因此,为了解决您的问题,当您从用户那里获取文本时,您应该检查所需的元素数量,而不是仅仅假设文本的格式正确。所以你会做类似的事情:

String[] dateStrings = date.split("/");

int day;
int month;
int year;
if (dateStrings.length == 3) {
    day = Integer.parseInt(dateStrings[0]);
    month = Integer.parseInt(dateStrings[1]);
    year = Integer.parseInt(dateStrings[2]);
}

同样适用于time