【问题标题】:Null pointer exception at TextView setText view is inflatedTextView setText 视图中的空指针异常被夸大
【发布时间】:2015-04-06 22:17:49
【问题描述】:

当我想将一个对象添加到我的列表/数据库时,我得到一个空指针异常。我正在膨胀对象的 xml 文件,但它找不到 TextView。异常发生在txtView.setText();

我的主要活动:

public class MainActivity extends ActionBarActivity {

protected PhoneDBHelper db;
List<Phone> list;
MyAdapter adapt;

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

    db = new PhoneDBHelper(this);
    list = db.getAllPhones();
    adapt = new MyAdapter(this, R.layout.list_inner_view, list);
    ListView listPhone = (ListView)findViewById(R.id.listView1);
    listPhone.setAdapter(adapt);
}

public void addPhoneNow(View v){
    EditText t = (EditText) findViewById(R.id.editText1);
    EditText author = (EditText) findViewById(R.id.editText);
    String s = t.getText().toString();
    String a = author.getText().toString();
    if(s.equalsIgnoreCase("")){
        Toast.makeText(this, "Enter the phone name first!", Toast.LENGTH_LONG);
    } else {
        Phone phone = new Phone(s, a);
        db.addPhone(phone);
        Log.d("phones", "data added");
        t.setText("");
        author.setText("");
        adapt.add(phone);
        adapt.notifyDataSetChanged();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private class MyAdapter extends ArrayAdapter<Phone>{
    Context context;
    List<Phone> phoneList = new ArrayList<Phone>();
    int layoutResourceId;
    public MyAdapter(Context context, int layoutResourceId, List<Phone> phoneList){
        super(context, layoutResourceId, phoneList);
        this.layoutResourceId = layoutResourceId;
        this.phoneList = phoneList;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_inner_view, parent, false);
        TextView textView = (TextView) convertView.findViewById(R.id.textBookView);
        Phone current = phoneList.get(position);
        textView.setText(current.getPhoneName() + " - " + current.getAuthor());

        return rowView;
    }
}

我的对象的 XML 文件:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textBookView"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

我忽略了什么吗?抱歉,如果这个问题已经被问过,但我找不到我的解决方案。

【问题讨论】:

    标签: java android android-layout nullpointerexception textview


    【解决方案1】:

    把你的getView方法改成这样:

     @Override
                public View getView(int position, View convertView, ViewGroup parent){
    
                    if(convertView == null)
                    {
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = inflater.inflate(R.layout.list_inner_view, parent, false);
                    }
    
                    TextView textView = (TextView) convertView.findViewById(R.id.textBookView);
                    Phone current = phoneList.get(position);
                    textView.setText(current.getPhoneName() + " - " + current.getAuthor());
    
                    return convertView;
                }
    

    convertView 是要重用的旧视图,如果可能的话。注意:您应该在使用前检查此视图是否为非空且类型合适。如果无法将此视图转换为显示正确的数据,则此方法可以创建一个新视图。异构列表可以指定其视图类型的数量,以便此视图始终是正确的类型(参见 getViewTypeCount() 和 getItemViewType(int))。我从这里得到这个http://developer.android.com/reference/android/widget/Adapter.html

    【讨论】:

    • 它仍然给出了一个空指针异常,它最初是在 setText,现在它找不到编辑的行。
    • 它现在在 'TextView textView = (TextView) convertView.findViewById(R.id.textBookView);'中给出异常
    • 你能把logcat的输出贴出来吗
    • 对不起,变量名不合逻辑
    • 您,先生,是个天才。非常感谢!奇迹般有效。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2012-04-20
    • 2022-01-01
    相关资源
    最近更新 更多