在此应用中添加代码后,该应用无法正常运行

时间:2018-11-02 15:14:38

标签: java android

error message screenshot

添加此代码后,该应用程序无法运行。问题是什么? 如果删除此代码,则该应用正常运行。

int i, num=0, num2=0;
    for(i=0; i<=23; i++) {
        if(result[i] == 1) {
            num++;
        }
    }
    int[] Time = new int[num-1];
    for(i=0; i<=23; i++) {
        if(result[i] == 1) {
            Time[num2]=i;
            num2++;
        }
    }

    Toast.makeText(getApplicationContext(), Arrays.toString(Time), Toast.LENGTH_LONG).show();

完整的代码是这个

package org.techtown.challenge09;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
ListView listView;
SingerAdapter adapter;
EditText Name;
EditText Start;
EditText End;

Button button;


int[] result = new int[24];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Name = findViewById(R.id.editText1);
    Start = findViewById(R.id.editText2);
    End = findViewById(R.id.editText3);

    adapter = new SingerAdapter();

    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String name = Name.getText().toString();
            String start = Start.getText().toString() + ":00";
            String end = End.getText().toString() + ":00";

            adapter.addItem(new SingerItem(name, start, end, R.drawable.human));


            int First = Integer.parseInt(Start.getText().toString());
            int Last = Integer.parseInt(End.getText().toString());
            for(int i=First; i<Last; i++) {
                result[i] = 1;
            }
        }

    });

    listView = findViewById(R.id.listView);

    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            SingerItem item = (SingerItem) adapter.getItem(position);
            Toast.makeText(getApplicationContext(), "선택 : " + item.getName(),
                    Toast.LENGTH_LONG).show();
        }
    });


    int i, num=0, num2=0;
    for(i=0; i<=23; i++) {
        if(result[i] == 1) {
            num++;
        }
    }
    int[] Time = new int[num-1];
    for(i=0; i<=23; i++) {
        if(result[i] == 1) {
            Time[num2]=i;
            num2++;
        }
    }

    Toast.makeText(getApplicationContext(), Arrays.toString(Time), Toast.LENGTH_LONG).show();
}

public class SingerAdapter extends BaseAdapter {
    ArrayList<SingerItem> items = new ArrayList<SingerItem>();

    @Override
    public int getCount() {
        return items.size();
    }

    public void addItem(SingerItem item) {
        items.add(item);
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        SingerItemView view = new SingerItemView(getApplicationContext());
        SingerItem item = items.get(position);
        view.setName(item.getName());
        view.setStart(item.getStart());
        view.setEnd(item.getEnd());
        view.setImage(item.getResId());

        return view;
    }
}

}

3 个答案:

答案 0 :(得分:0)

点击按钮后,将调用此代码:

int First = Integer.parseInt(Start.getText().toString());
int Last = Integer.parseInt(End.getText().toString());
for(int i=First; i<Last; i++) {
    result[i] = 1;
}

您正试图在点击按钮且result为空之前调用此代码:

int i, num=0, num2=0;
for(i=0; i<=23; i++) {
    if(result[i] == 1) {
        num++;
    }
}
int[] Time = new int[num-1];
for(i=0; i<=23; i++) {
    if(result[i] == 1) {
        Time[num2]=i;
        num2++;
    }
}

在您的onClickListener

中移动该代码

答案 1 :(得分:0)

由于您尚未发布错误日志,所以我只是在猜测。在此代码中:

if (x == 0 && y == 0) {
// code to be called
}

如果int i, num=0, num2=0; for(i=0; i<=23; i++) { if(result[i] == 1) { num++; } } int[] Time = new int[num-1]; 中没有等于result[]的元素,则1将保留其初始值num
因此,之后您将0初始化为Time
这将引发类似int[-1]

的异常

答案 2 :(得分:0)

像这样更改代码后,该应用程序即可正常工作!谢谢大家。

public class MainActivity extends AppCompatActivity {
ListView listView;
SingerAdapter adapter;
EditText Name;
EditText Start;
EditText End;

Button button;

//가능한 시간 값 0, 가능하지 않은 시간 값 1
int[] result = new int[24];

//버튼의 클릭 여부 확인 변수
int click=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Name = findViewById(R.id.editText1);
    Start = findViewById(R.id.editText2);
    End = findViewById(R.id.editText3);

    adapter = new SingerAdapter();

    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String name = Name.getText().toString();
            String start = Start.getText().toString() + ":00";
            String end = End.getText().toString() + ":00";

            adapter.addItem(new SingerItem(name, start, end, R.drawable.human));

            //가능하지 않은 시간 체크
            int First = Integer.parseInt(Start.getText().toString());
            int Last = Integer.parseInt(End.getText().toString());
            for(int i=First; i<Last; i++) {
                result[i] = 1;
            }
        }

    });

    listView = findViewById(R.id.listView);

    listView.setAdapter(adapter);

    //아이템 클릭 리스너 설정
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            SingerItem item = (SingerItem) adapter.getItem(position);
            Toast.makeText(getApplicationContext(), "선택 : " + item.getName(),
                    Toast.LENGTH_LONG).show();
            //버튼이 클릭되었음을 표시
            click=1;
        }
    });

    if(click == 1) {
        //가능한 시간 Time[i]에 저장
        int num=0, num2=0;
        for(int i=0; i<=23; i++) {
            if(result[i] == 1) {
                num++;
            }
        }
        int[] Time = new int[num-1];
        for(int i=0; i<=23; i++) {
            if(result[i] == 1) {
                Time[num2]=i;
                num2++;
            }
        }

        Toast.makeText(getApplicationContext(), Arrays.toString(Time), Toast.LENGTH_LONG).show();
    }
}
public class SingerAdapter extends BaseAdapter {
    ArrayList<SingerItem> items = new ArrayList<SingerItem>();

    @Override
    public int getCount() {
        return items.size();
    }

    public void addItem(SingerItem item) {
        items.add(item);
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        SingerItemView view = new SingerItemView(getApplicationContext());
        SingerItem item = items.get(position);
        view.setName(item.getName());
        view.setStart(item.getStart());
        view.setEnd(item.getEnd());
        view.setImage(item.getResId());

        return view;
    }
}
}