1

我正在尝试编写一个笔记管理应用程序,当我添加一些新笔记时,删除它们然后尝试添加新笔记,该应用程序在 setcontentview() 上崩溃。我添加了日志,可以通过删除第一个editText然后添加一个新的来轻松触发它。

public class Mainpage extends AppCompatActivity {

private LinearLayout MyLayout;
private ScrollView sv;
private List<EditText> et = new ArrayList<EditText>();
private List<EditText> ct = new ArrayList<EditText>();
private List<TextView> tv = new ArrayList<TextView>();
private List<CheckBox> cb = new ArrayList<CheckBox>();
private Button btn;
final Context context = this;
private CountDownTimer timer;
boolean isWaiting;
int last_added;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_mainpage);
    last_added = 0;
    //btn = (Button) findViewById(R.id.btn);
    //btn.setVisibility(View.INVISIBLE);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_mainpage, menu);
    sv = new ScrollView(this);
    MyLayout = new LinearLayout(this);
    MyLayout.setOrientation(LinearLayout.VERTICAL);
    sv.addView(MyLayout);
    timer = new CountDownTimer(500,500) {
        @Override
        public void onTick(long millisUntilFinished) {
        }
        @Override
        public void onFinish() {
            isWaiting = false;
        }
    };
    btn = new Button(this);
    btn.setText("Accept");
    btn.setVisibility(View.INVISIBLE);
    MyLayout.addView(btn);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    else if (id == R.id.action_add) {


        et.add(new EditText(this)) ;
        tv.add(new TextView(this));
        ct.add(new EditText(this));
        cb.add(new CheckBox(this));
        cb.get(ct.size() -1).setVisibility(View.INVISIBLE);
        ct.get(ct.size() -1).setVisibility(View.INVISIBLE);
        Calendar cal = Calendar.getInstance();
        String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
        tv.get(tv.size() -1).setText("created on" + date);
        et.get(et.size() - 1).setText("weeeeeeeeeee~!");

        MyLayout.addView(et.get(et.size() - 1));
        MyLayout.addView(ct.get(ct.size() -1));
        MyLayout.addView(tv.get(tv.size() - 1));
        MyLayout.addView(cb.get(cb.size() - 1));

        this.setContentView(sv);


        et.get(last_added).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i=0;i<et.size();i++)
            {
                if(et.get(i) == v)
                {
                    last_added = i + 1;
                    break;
                }
            }

            if(isWaiting) {
                isWaiting = false;
                timer.cancel();
                // TODO: open a new EditText
                if(ct.get(last_added -1).isShown())
                {
                    ct.get(last_added -1).setVisibility(View.INVISIBLE);
                }
                else

                {
                    ct.get(last_added -1).setVisibility(View.VISIBLE);
                }


                }
            else {
                        isWaiting = true;
                        timer.start();
                    }
                }
            });
        last_added++;
        return true;
    }

    else if (id == R.id.action_delete) {
        btn.setVisibility(View.VISIBLE);
        for (int i = 0; i < cb.size(); i++) {
            cb.get(i).setVisibility(View.VISIBLE);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int j=0; j < cb.size();j++)
                {
                    if(cb.get(j).isChecked())
                    {
                        et.get(j).setVisibility(View.GONE);
                        tv.get(j).setVisibility(View.GONE);
                        ct.get(j).setVisibility(View.GONE);
                        cb.get(j).setVisibility(View.GONE);
                        et.remove(j);
                        tv.remove(j);
                        ct.remove(j);
                        cb.remove(j);
                    }
                }
                btn.setVisibility(View.INVISIBLE);
                for (int i = 0; i < cb.size(); i++) {
                    cb.get(i).setVisibility(View.INVISIBLE);
                }
            }
        });

        this.setContentView(sv);

        return true;
    }
    else if (id == R.id.action_search) {
        return true;
    }
    else if (id == R.id.action_send) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

日志

10-16 11:10:34.203  13030-13030/com.example.user_pc.notes E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user_pc.notes, PID: 13030
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.user_pc.notes.Mainpage.onOptionsItemSelected(Mainpage.java:125)
        at android.app.Activity.onMenuItemSelected(Activity.java:2908)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:325)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
        at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:609)
        at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:619)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
4

2 回答 2

0

解决它 。当我尝试添加新的 EditText 时,最后添加的值太高了。

公共类 Mainpage 扩展 AppCompatActivity {

private LinearLayout MyLayout;
private ScrollView sv;
private List<EditText> et = new ArrayList<EditText>();
private List<EditText> ct = new ArrayList<EditText>();
private List<TextView> tv = new ArrayList<TextView>();
private List<CheckBox> cb = new ArrayList<CheckBox>();


private Button btn;
final Context context = this;
private CountDownTimer timer;
boolean isWaiting;
int last_added;
int num_removed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_mainpage);
    last_added = 0;
    //btn = (Button) findViewById(R.id.btn);
    //btn.setVisibility(View.INVISIBLE);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_mainpage, menu);
    sv = new ScrollView(this);
    MyLayout = new LinearLayout(this);
    MyLayout.setOrientation(LinearLayout.VERTICAL);
    sv.addView(MyLayout);
    timer = new CountDownTimer(500,500) {
        @Override
        public void onTick(long millisUntilFinished) {
        }
        @Override
        public void onFinish() {
            isWaiting = false;
        }
    };
    btn = new Button(this);
    btn.setText("Accept");
    btn.setVisibility(View.INVISIBLE);
    MyLayout.addView(btn);
    ///search shit

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    else if (id == R.id.action_add) {


        et.add(new EditText(this)) ;
        tv.add(new TextView(this));
        ct.add(new EditText(this));
        cb.add(new CheckBox(this));
        cb.get(ct.size() -1).setVisibility(View.INVISIBLE);
        ct.get(ct.size() -1).setVisibility(View.INVISIBLE);
        Calendar cal = Calendar.getInstance();
        String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
        tv.get(tv.size() -1).setText("created on" + date);
        et.get(et.size() - 1).setText("weeeeeeeeeee~!");
        MyLayout.addView(et.get(et.size() - 1));
        MyLayout.addView(ct.get(ct.size() -1));
        MyLayout.addView(tv.get(tv.size() - 1));
        MyLayout.addView(cb.get(cb.size() - 1));

        this.setContentView(sv);

        et.get(last_added).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i=0;i<et.size();i++)
            {
                if(et.get(i) == v)
                {
                    last_added = i + 1;
                    break;
                }
            }

            if(isWaiting) {
                isWaiting = false;
                timer.cancel();
                // TODO: open a new EditText
                if(ct.get(last_added -1).isShown())
                {
                    ct.get(last_added -1).setVisibility(View.INVISIBLE);
                }
                else

                {
                    ct.get(last_added -1).setVisibility(View.VISIBLE);
                }


                }
            else {
                        isWaiting = true;
                        timer.start();
                    }
                }
            });
        last_added++;
        return true;
    }

    else if (id == R.id.action_delete) {
        num_removed = 0;
        btn.setVisibility(View.VISIBLE);
        for (int i = 0; i < cb.size(); i++) {
            cb.get(i).setVisibility(View.VISIBLE);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            int checked =0;
            @Override
            public void onClick(View v) {
                while(true)
                {
                    for(int j=0;j< cb.size();j++)
                    {
                        if(cb.get(j).isChecked())
                        {
                            checked++;
                        }

                    }
                    if(checked == 0)
                    {
                        break;
                    }
                    for(int i=0;i<cb.size();i++)
                    {
                        if(cb.get(i).isChecked())
                        {
                            et.get(i).setVisibility(View.GONE);
                            tv.get(i).setVisibility(View.GONE);
                            ct.get(i).setVisibility(View.GONE);
                            cb.get(i).setVisibility(View.GONE);
                            et.remove(i);
                            tv.remove(i);
                            ct.remove(i);
                            cb.remove(i);
                            last_added--;
                        }
                    }
                    checked = 0;
                }

                btn.setVisibility(View.INVISIBLE);
                for (int i = 0; i < cb.size(); i++) {
                    cb.get(i).setVisibility(View.INVISIBLE);
                }
            }
        });

        this.setContentView(sv);

        return true;
    }
    else if (id == R.id.action_search) {
        btn.setVisibility(View.VISIBLE);
        for (int i = 0; i < cb.size(); i++) {
            cb.get(i).setVisibility(View.VISIBLE);
        }

        return true;
    }

    else if (id == R.id.action_send)
    {


        btn.setVisibility(View.VISIBLE);

        for (int i = 0; i < cb.size(); i++)
        {
            cb.get(i).setVisibility(View.VISIBLE);
            cb.get(i).setChecked(false);
        }
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                String [] to = new String[]{};

                for(int j=0; j < cb.size();j++)
                {
                    if(cb.get(j).isChecked())
                    {
                        sendEmail(et.get(j).getText().toString(),ct.get(j).getText().toString());
                    }

                }
                btn.setVisibility(View.INVISIBLE);
                for (int i = 0; i < cb.size(); i++)
                {
                    cb.get(i).setVisibility(View.INVISIBLE);
                }
        }});

        this.setContentView(sv);

        return true;
    }
    return super.onOptionsItemSelected(item);
}




protected void sendEmail(String subject,String Context)
{

    Intent emailIntent = new Intent(Intent.ACTION_SEND);

    emailIntent.putExtra(Intent.EXTRA_SUBJECT,subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT,Context);
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent,"Email"));
}

}

于 2015-10-16T14:07:13.313 回答
0

1)您的错误错误显示在 125 中您有 IndexOutOfBoundsException 异常。

at com.example.user_pc.notes.Mainpage.onOptionsItemSelected(Mainpage.java:125)

您在其中一个数组上调用 get 函数。列表中有一项。最大索引必须为 0,但您调用 1 它会引发 IndexOutOfBoundsException。

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

2)你不需要像你一样创建额外的arraylist

private List<EditText> et = new ArrayList<EditText>();

LinearLayout 有以下方法。你可以从你的布局中得到它们。

   getChildAt());
   getChildCount();

3) 您不需要在 ScrollView 上调用以下方法。它必须自动

this.setContentView(sv);

4)最后你的代码非常糟糕。尝试使用标准方式。阅读安卓教程https://developer.android.com/guide/topics/ui/index.html

于 2015-10-16T12:32:04.177 回答