简体   繁体   中英

Null Object Reference Android Studio

I call this line

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

and the error appears as followed.

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

The file of the string array is array.xml, in values.

<?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>

This is my full code for this layout.

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);
    }
}

I moved the String[] stringList and the List to the onCreate, now it works.

New Code:

@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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM