null对象引用android studio

dfty9e19  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(321)

我叫这条线

String[] stringList = getResources().getStringArray(R.array.food_array);

错误如下所示。

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.textchanger/com.example.textchanger.SecondaryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

字符串数组的文件是array.xml,以值表示。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="food_array">
        <item> Cake </item>
        <item> Rats </item>
        <item> Grass </item>
    </string-array>
</resources>

这是我这个布局的完整代码。

package com.example.textchanger;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SecondaryActivity extends AppCompatActivity {

    // Initialize
    ListView listView;
    String[] stringList = getResources().getStringArray(R.array.food_array);
    List<String> stringArrayList = new ArrayList<String>(Arrays.asList(stringList));

    ArrayAdapter<String> adapter;

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

        // Assign ListView
        listView = findViewById(R.id.list_view);

        // Adapter
        adapter = new ArrayAdapter<>(SecondaryActivity.this
        , android.R.layout.simple_list_item_1,  stringArrayList);

        // On listview adapter set
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext()
                        ,adapter.getItem(position), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Initialize menu inflater
        MenuInflater menuInflater = getMenuInflater();

        // Inflate Menu
        menuInflater.inflate(R.menu.menu_search, menu);
        // Initialize menu item
        MenuItem menuItem = menu.findItem(R.id.search_view);
        // Initialize Search View
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);

        // Listen out for text
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Filter Array
                adapter.getFilter().filter(newText);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }
}
avwztpqn

avwztpqn1#

我将string[]stringlist和list移到oncreate,现在它可以工作了。
新代码:

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

    // Get Resource
    String[] stringList = getResources().getStringArray(R.array.food_array);
    List<String> stringArrayList = new ArrayList<String>(Arrays.asList(stringList));

相关问题