2015-04-06 108 views
1

當我想將對象添加到我的列表/數據庫時,出現空指針異常。我膨脹對象的XML文件,但它找不到TextView。 txtView.setText();發生異常。TextView setText視圖中的空指針異常被誇大

我的MainActivity:

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"/> 

我俯瞰的東西嗎?對不起,如果這個問題已經被問到,但我找不到我的解決方案。

回答

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

+0

它仍然給出一個nullpointerexception,它首先在setText,現在它找不到編輯行。 – user2912900

+0

你是什麼意思的編輯線? – Arlind

+0

它現在在'TextView textView =(TextView)convertView.findViewById(R.id.textBookView);' – user2912900