簡體   English   中英

EditText在.getText()上引發NullPointerException

[英]EditText Throws NullPointerException On .getText()

在我的應用程序中,我動態創建了EditText,並在其中輸入了文本。 然后onCLick,ArrayList獲取EditTexts中的所有文本,然后進行序列化。 我正好在for循環中獲取EditTexts的文本並將其添加到ArrayList中。

我想我知道發生了什么,但不知道如何解決。 我的理論是存在一個問題,在此問題中,我在這里創建了一個包含EditTexts的變量:

etIndex=(EditText)newView.findViewById(i+1000);

我的想法是在說newView時遇到問題。 這是膨脹視圖的名稱,但是問題是在我再次使布局膨脹的每次單擊中都將其擦除。 如果這沒有意義,請閱讀下面包含的Java類,這將是100%有意義的。

這是我調用.getText()的地方,以及發生NullPointerException的行:

listOptions.add(etIndex.getText().toString());

這是我制作EditText的地方:

LayoutInflater inflater = LayoutInflater.from(view.getContext());
                View newView = inflater.inflate(R.layout.additem, null);
                listItem = (LinearLayout)newView.findViewById(R.id.lladdItem);
                listItem.setId(numOfItems);
                tvItem = (TextView)newView.findViewById(R.id.tvItem);
                tvItem.setText(numOfItems + ".");
                etItem = (EditText)newView.findViewById(R.id.etItem);
                etItem.setId(numOfItems+1000);
                etItem.setHint("List Item " + numOfItems);

如果您希望這是我的整個Java文件:

package com.frostbytedev.randomgenie;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * Created by Steven on 6/23/13.
     */
    public class NewList extends Activity implements View.OnClickListener{
        java.util.List<Integer> listOfET = new ArrayList<Integer>();
        java.util.List<String> listOptions = new ArrayList<String>();
        View newView;
        LinearLayout insideScroll, listItem;
        ScrollView svItems;
        TextView tvItem;
        EditText FileName, etItem, etItem1, etIndex;
        Button Add, Remove, Save;
        int numOfItems = 1, i;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newlist);
            initialize();
        }

        private void initialize() {
            FileName=(EditText)findViewById(R.id.etTitle);
            Save=(Button)findViewById(R.id.bSave);
            Add=(Button)findViewById(R.id.bAdd);
            Remove=(Button)findViewById(R.id.bRemove);
            insideScroll=(LinearLayout)findViewById(R.id.insideScroll);
            etItem1=(EditText)findViewById(R.id.etItem1);
            Save.setOnClickListener(this);
            Add.setOnClickListener(this);
            Remove.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.bAdd:
                    numOfItems += 1;
                    LayoutInflater inflater = LayoutInflater.from(view.getContext());
                    View newView = inflater.inflate(R.layout.additem, null);
                    listItem = (LinearLayout)newView.findViewById(R.id.lladdItem);
                    listItem.setId(numOfItems);
                    tvItem = (TextView)newView.findViewById(R.id.tvItem);
                    tvItem.setText(numOfItems + ".");
                    etItem = (EditText)newView.findViewById(R.id.etItem);
                    etItem.setId(numOfItems+1000);
                    etItem.setHint("List Item " + numOfItems);
                    insideScroll.addView(newView);

                    break;
                case R.id. bRemove:
                    if(numOfItems==1){

                    } else {
                        View v = findViewById(numOfItems);
                        ((LinearLayout)v.getParent()).removeView(v);
                        numOfItems -= 1;
                    }
                    break;

                case R.id.bSave:
                    IndexList();
                    try {
                        SaveList();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Intent OpenList = new Intent(getApplicationContext(), ListRandom.class);
                    OpenList.putExtra("Filename", FileName+".txt");
                    startActivity(OpenList);
                    break;
            }
        }

        private void SaveList() throws IOException {
            String filename = FileName.getText().toString()+".txt";
            FileOutputStream fos;
            try {
                fos = openFileOutput(filename,Context.MODE_PRIVATE);
                ObjectOutputStream out = new ObjectOutputStream(fos);
                out.writeObject(listOptions);
                out.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }
        private void IndexList() {
            listOptions.add(etItem1.getText().toString());
               for(i=1;i<numOfItems;i++){
                   etIndex=(EditText)newView.findViewById(i+1000);
                   listOptions.add(etIndex.getText().toString());
               }
        }

    }

第一次創建id numOfItems是2,而不是1,如果是1002 ,則會導致第一個id。 然后在循環中,嘗試獲取ID 1001 在日志語句中輸出所有創建的ID,這將幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM