Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-奇怪“;java.lang.NullPointerException“;_Android_Nullpointerexception - Fatal编程技术网

Android 安卓-奇怪“;java.lang.NullPointerException“;

Android 安卓-奇怪“;java.lang.NullPointerException“;,android,nullpointerexception,Android,Nullpointerexception,您好,我正在尝试制作一个有搜索框的应用程序,我已经编写了一些代码,但是当我尝试在AutoCompleteTextView中键入第一个字母时,它会给我一个错误(java.lang.NullPointerException),没有任何原因说明是什么导致了它 mymains.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re

您好,我正在尝试制作一个有搜索框的应用程序,我已经编写了一些代码,但是当我尝试在AutoCompleteTextView中键入第一个字母时,它会给我一个错误(java.lang.NullPointerException),没有任何原因说明是什么导致了它

mymains.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<AutoCompleteTextView
 android:id="@+id/acTV"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Scrivi">
<requestFocus />
</AutoCompleteTextView>
<ListView android:id="@+id/list" 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent"
 android:fadingEdge="none" 
 android:fastScrollEnabled="true">
</ListView>
</LinearLayout>

My listitem_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<TextView
 android:id="@+id/textView1"
 android:text="TextView"
 android:layout_height="wrap_content"
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:layout_width="fill_parent">
</TextView>
<TextView
 android:text="TextView"
 android:id="@+id/textView2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
</TextView>
</LinearLayout>

我的代码:

public class Search extends Activity {
    AutoCompleteTextView acTV;
    ListView lview;

    String[] first = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};

    String[] second = {"Uno", "Due", "Tre", "Quattro", "Cinque", "Sei", "Sette", "Otto", "Nove", "Dieci"};
    int textlength = 0;
    ArrayList<String> first_sort = new ArrayList<String>();
    ArrayList<String> second_sort = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mains);

        acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
        lview = (ListView) findViewById(R.id.list);
        lview.setAdapter(new MyCustomAdapter(first, second));

        lview.setClickable(true);
        lview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

                Intent intent = new Intent(Search.this, Details.class);
                startActivity(intent);
            }

            {
            }
        });

        lview.setTextFilterEnabled(true);
        acTV.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {

                textlength = acTV.getText().length();
                first_sort.clear();
                second_sort.clear();
                for (int i = 0; i < first.length; i++) {
                    if (textlength <= first[i].length()) {
                        if (acTV.getText().toString().equalsIgnoreCase((String) first[i].subSequence(0, textlength))) {
                            first_sort.add(first[i]);
                            second_sort.add(first[i]);
                        }
                    }
                }

                lview.setAdapter(new MyCustomAdapter(first_sort, second_sort));

            }
        });
    }

    class MyCustomAdapter extends BaseAdapter {

        String[] data_first;
        String[] data_second;

        {

        }

        MyCustomAdapter(String[] first, String[] second) {
            data_first = first;
            data_second = second;
        }

        MyCustomAdapter(ArrayList<String> first, ArrayList<String> second) {
            for (int i = 0; i < first.size(); i++) {
                data_first[i] = first.get(i);
                data_second[i] = second.get(i);
            }

        }

        public int getCount() {
            return data_first.length;
        }

        public String getItem(int position) {
            return null;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = getLayoutInflater();
            View row;

            row = inflater.inflate(R.layout.listitem_row, parent, false);

            TextView textview = (TextView) row.findViewById(R.id.textView1);
            TextView textview1 = (TextView) row.findViewById(R.id.textView2);

            textview.setText(data_first[position]);
            textview1.setText(data_second[position]);

                return (row);

           }
        }
    }
}
公共类搜索扩展活动{
自动完成文本视图acTV;
列表视图lview;
String[]first={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
字符串[]秒={“Uno”、“Due”、“Tre”、“Quattro”、“Cinque”、“Sei”、“Sette”、“Otto”、“Nove”、“Dieci”};
int textlength=0;
ArrayList first_sort=新建ArrayList();
ArrayList second_sort=新的ArrayList();
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局干线);
acTV=(AutoCompleteTextView)findviewbyd(R.id.acTV);
lview=(ListView)findviewbyd(R.id.list);
setAdapter(新的MyCustomAdapter(第一个,第二个));
lview.setClickable(true);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(适配器视图适配器、视图视图、整型位置、长id){
意向意向=新意向(Search.this,Details.class);
星触觉(意向);
}
{
}
});
lview.setTextFilterEnabled(true);
acTV.addTextChangedListener(新的TextWatcher(){
公共无效后文本已更改(可编辑){
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
public void onTextChanged(字符序列,int start,
前整数,整数计数){
textlength=acTV.getText().length();
第一次排序。清除();
第二种排序。清除();
for(int i=0;iif(textlength我猜
排序
参数之一是
null

经OP注释确认,
第一次排序
null


根据,您需要:

  • 将所有类变量的值(包括
    onSaveInstanceState(Bundle)
    中的静态变量)保存到Bundle中
  • onCreate(bundle)
    中的bundle(如果不是
    null
    )还原变量
  • 另见:


    …类似这样的(未测试):


    哪一行代码是
    Search.java
    114行?data_first[i]=first.get(i);这就是我的全部代码,它没有分为两个活动,而是一个活动。我没有看到任何关于它格式不正确的错误。谢谢你的回答,这是我的第一个应用程序,我没有你想象的那么好。你能帮我写缺少的代码或提供更好的线索吗?再次感谢你。另外,我使用android:configChanges=“键盘|键盘隐藏|方向”在Manifest中而不是onSave和onRestore中。它也可以工作。还感谢其他帮助。当我说如果你可以帮助我编写代码时,我的意思是如果可以的话,我想知道如何解决NullPointerException。我已经根据需要修改了此示例代码,但我不明白为什么我的代码不工作。我有两个字符串[],而不是一个字符串[]和一个int[],如示例中所示。这可能是导致错误的原因吗?
    public class Search extends Activity {
        // snip...
    
        public void onCreate(Bundle savedInstanceState) {
            // snip...
    
            if (first_sort == null) {
                first_sort = savedInstanceState.getParcelableArrayList("first_sort");
            }
    
            if (second_sort == null) {
                second_sort = savedInstanceState.getParcelableArrayList("second_sort");
            }
        }
    
        // snip...
    
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putParcelableArrayList("first_sort", first_sort);
            outState.putParcelableArrayList("second_sort", second_sort);
        }
    }