以编程方式加载的XML会给出nullpointers?

时间:2014-11-25 20:40:15

标签: android android-layout

基本上:以编程方式加载到另一个布局的xml文件为我提供了“空指针”......但只是在某些时候。

更具体地说:parent.xml如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout >

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout" >

    <!-- 2 columns -->
    <TableRow
        android:id="@+id/table_header" >

        <TextView
            android:id="@+id/title"
            android:text="Number" />

        <TextView
            android:id="@+id/attached_title"
            android:text="Info" />

        <!-- ******* 1 **********************-->
        <TableRow
            android:id="@+id/row_1" >

            <TextView
                android:id="@+id/num1"
                android:text="1"/>

            <RelativeLayout>
                <ListView
                    android:id="@+id/listView_1">
                </ListView>
            </RelativeLayout>
        </TableRow>

    <!-- ****** 2 ****************-->
    <TableRow
        android:id="@+id/row_2">

        <TextView
            android:id="@+id/num2"
            android:text="2"/>

        <RelativeLayout>
            <ListView
                android:id="@+id/listView_2>
            </ListView>
        </RelativeLayout>

    </TableRow>
   </TableLayout>
  </LinearLayout>
 </ScrollView>

child.xml如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

<LinearLayout>


    <!-- ********* Module ***************************-->
        <RelativeLayout>
            <Spinner
                android:id="@+id/chc"/>
        </RelativeLayout>


        <RelativeLayout>

            <EditText
                android:id="@+id/editTextResult_name">
            </EditText>
        </RelativeLayout>
       </LinearLayout>
      </ScrollView>

在使用这些xml文件的活动中,我从onStart调用populate()方法:

private void populate(Thing thing, int num){
    ListView listView = findListViewByNum(num);
    CustomAdapter adapter = new CustomAdapter(this, thing, num);
    listView.setAdapter(adapter);
}

这是我想要做的,但是什么行不通。在完成对populate()的所有调用之后,我试图在onStart()的底部调用此代码。据我所知,此时所有视图都应该被夸大,并且活动应该可以访问XML元素,因为我在这里使用它们:

public void disable(View v){
  ListView curr = (ListView) findViewById(R.id.listview_1);
  EditText name = (EditText) curr.findViewById(R.id.editTextResult_name);

  Spinner chc = (Spinner) curr.findViewById(R.id.chc);
  String type = chc.getSelectedItem().toString(); // null pointer here.

  if (type.equalsIgnoreCase("DISABLED")){
    name.setEnabled(false);
  }
}

奇怪的是,按下按钮后,完全相同的代码会被调用,然后就可以了。它只是在我直接调用它时才起作用。

这是相同的东西,但它有效:

public void editt1(View v){
  ListView curr = (ListView) findViewById(R.id.listview_1);
  Spinner chc = (Spinner) curr.findViewById(R.id.chc); // finds the right thing
  String type = chc.getSelectedItem().toString(); // no null pointer
  doThing(1, type);

}

什么不起作用:在disable(),name和chc都为null,导致我指示它的nullpointer。

我读过的内容

我的问题

  • 我期待着,一旦屏幕上绘制了所有内容,我的代码就可以完全访问每个XML元素。这是不正确的吗?

  • 我是否需要以不同的方式处理“嵌套”结构,例如表格内的RelativeLayout内的EditText?

  • 为什么不起作用?

*****编辑*****

根据要求添加CustomAdapter代码:

public class CustomAdapter extends BaseAdapter implements ListAdapter {

private Thing thing;
private LayoutInflater inflater;
private EditText name;
private Spinner chc;
private Context context;
private View row;

public CustomAdapter(Activity context, Thing thing) {
  super();
  this.thing = thing;
  this.inflater = context.getLayoutInflater();
  this.context = context;
}

@Override
public View getView(int pos, View convertView, ViewGroup parent){
  row = convertView;
  if (row == null){
    row = inflater.inflate(R.layout.child, parent, false);
  }

  name = (EditText) row.findViewById(R.id.editTextResult_name);
  name.setText(thing.getName());

  chc = (Spinner) row.findViewById(R.id.chc);
  ArrayAdapter chcAdapter = (ArrayAdapter) chc.getAdapter();

  int spinnerPosition =    chc_adapter.getPosition(thing.pos));
  chc.setSelection(spinnerPosition);
  chc.setOnItemSelectedListener(listener);

  return row;

}

private AdapterView.OnItemSelectedListener listener = new   
                       AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
  String chosen = adapterView.getItemAtPosition(pos).toString();

  if (chosen.equalsIgnoreCase("blank")){}

    // this doesn't work
    Toast.makeText(context, "name: "+ name.getText().toString(), Toast.LENGTH_SHORT).show();
    Toast.makeText(context, "before nameText isenabled: "+ name.isEnabled(), Toast.LENGTH_SHORT).show();
    name.setEnabled(false);
    name.setFocusableInTouchMode(false);
    Toast.makeText(context, "after nameText isEnabled: "+ name.isEnabled(), Toast.LENGTH_SHORT).show();

  } else {
    makeControllerButtonVisible(selected, port);

    //this part doesn't work, not sure why.
    Toast.makeText(context, "name: "+ name.getText().toString(), Toast.LENGTH_SHORT).show();
    Toast.makeText(context, "before name isEnabled: "+ name.isEnabled(), Toast.LENGTH_SHORT).show();
    name.setEnabled(true);
    name.setFocusableInTouchMode(true);
    Toast.makeText(context, "after name isEnabled: "+ name.isEnabled(), Toast.LENGTH_SHORT).show();

  }
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
};


}

0 个答案:

没有答案