LayoutInflater的NullPointerException问题

Android 码拜 9年前 (2015-10-23) 1290次浏览
LayoutInflater的问题,想要实现输入框输入后点击按钮添加按钮到已有的布局,LayoutInflate的使用方法我都试过,log给出的错误信息我也都查看了,看不懂,但是就它高亮显示的地方我都查看了,但是就是找不出问题所在,只要输入完成后添加的时候就会报错,

Log信息:

10-19 10:52:18.621    1269-1269/com.ghost.ft_searches E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.ghost.ft_searches.FT_Searches.makeTagGUI(FT_Searches.java:73)
            at com.ghost.ft_searches.FT_Searches.refreshButtons(FT_Searches.java:50)
            at com.ghost.ft_searches.FT_Searches.makeTag(FT_Searches.java:65)
            at com.ghost.ft_searches.FT_Searches.access$200(FT_Searches.java:23)
            at com.ghost.ft_searches.FT_Searches$1.onClick(FT_Searches.java:87)
            at android.view.View.performClick(View.java:2485)
            at android.view.View$PerformClick.run(View.java:9080)

下面是程序的代码

public class FT_Searches extends Activity{
    private SharedPreferences savedSearches;
    private TableLayout tag_tl;
    private EditText query_et;
    private EditText tag_et;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ft__searches);
        //
        savedSearches = getSharedPreferences("savedSearches",MODE_PRIVATE);
        tag_tl = (TableLayout) findViewById(R.id.tag_tl);
        query_et = (EditText) findViewById(R.id.query_et);
        tag_et = (EditText) findViewById(R.id.tag_et);
        Button save_bt = (Button) findViewById(R.id.save_bt);
        save_bt.setOnClickListener(save_btListener);
        Button clear_bt = (Button) findViewById(R.id.clear_bt);
        clear_bt.setOnClickListener(clear_btListener);
        //
        refreshButtons(null);
    }
    private void refreshButtons(String newTag){
        Set<String> strings = savedSearches.getAll().keySet();
        String[] tags = strings.toArray(new String[strings.size()]);
        Arrays.sort(tags, String.CASE_INSENSITIVE_ORDER);
        //
        if (newTag != null){
            makeTagGUI(newTag, Arrays.binarySearch(tags, newTag));
        }
        else {
            for (int index = 0; index < tags.length; ++index)
                makeTagGUI(tags[index], index);
        }
    }
    private void makeTag(String query, String tag){
        String originalQuery = savedSearches.getString(tag, null);
        SharedPreferences.Editor preferencesEditor = savedSearches.edit();
        preferencesEditor.putString(tag, query);
        preferencesEditor.apply();
        //
        if (originalQuery == null)
        {
            refreshButtons(tag);
        }
    }
    private void makeTagGUI(String tag, int index){
        LayoutInflater inflater = getLayoutInflater();
        View newTagView = inflater.inflate(R.layout.new_tag_sv, null);
        //
        Button tag_nbt = (Button) findViewById(R.id.tag_nbt);
        tag_nbt.setText(tag);
        tag_nbt.setOnClickListener(tag_nbtListener);
        //
        Button edit_nbt = (Button) findViewById(R.id.edit_nbt);
        edit_nbt.setOnClickListener(edit_nbtListener);
        tag_tl.addView(newTagView, index);
    }
    private void clear_bt(){
        tag_tl.removeAllViews();
    }
    public View.OnClickListener save_btListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (query_et.getText().length()>0 && tag_et.getText().length()>0){
                makeTag(query_et.getText().toString(), tag_et.getText().toString());
                query_et.setText("");
                tag_et.setText("");
                //
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(tag_et.getWindowToken(), 0);
            }
            else {
                AlertDialog.Builder builder =
                        new AlertDialog.Builder(FT_Searches.this);
                builder.setTitle(R.string.missingTitle);
                builder.setPositiveButton(R.string.OK, null);
                builder.setMessage(R.string.missingMessage);
                //
                AlertDialog errorDialog = builder.create();
                errorDialog.show();
            }
        }
    };
    public View.OnClickListener clear_btListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder =
                    new AlertDialog.Builder(FT_Searches.this);
            builder.setTitle(R.string.missingTitle);
            builder.setPositiveButton(R.string.erase,
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int button) {
                    clear_bt();
                    SharedPreferences.Editor preferencesEditor = savedSearches.edit();
                    preferencesEditor.clear();
                    preferencesEditor.apply();
                }
            });
            builder.setCancelable(true);
            builder.setNegativeButton(R.string.cancel, null);
            builder.setMessage(R.string.missingMessage);
            AlertDialog confirmDialog = builder.create();
            confirmDialog.show();
        }
    };
    public View.OnClickListener tag_nbtListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String buttonText = ((Button)v).getText().toString();
            String query = savedSearches.getString(buttonText, null);
            String urlString = getString(R.string.searchUrl) + query;
            Intent getURL = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
            startActivity(getURL);
        }
    };
    public View.OnClickListener edit_nbtListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TableRow buttonTableRow = (TableRow) v.getParent();
            Button searchButton = (Button) buttonTableRow.findViewById(R.id.tag_nbt);
            String tag = searchButton.getText().toString();
            tag_et.setText(tag);
            query_et.setText(savedSearches.getString(tag, null));
        }
    };
}
解决方案:40分
错误信息很清楚了makeTagGUI方法中某个控件为空,我估计是你找btn没找到

Button tag_nbt = (Button) findViewById(R.id.tag_nbt);
        Button edit_nbt = (Button) findViewById(R.id.edit_nbt);

这两句估计应该是

Button tag_nbt = (Button) newTagView.findViewById(R.id.tag_nbt);
        Button edit_nbt = (Button) newTagView.findViewById(R.id.edit_nbt);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明LayoutInflater的NullPointerException问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)