无法在ListView中

时间:2017-04-30 17:12:52

标签: android listview

这是为了一个班级的项目。我打算在事后继续并完成其他功能。

想法:这个活动假设有几个输入,主要是假设是一个ListView,它将根据用户在numCombos中键入的内容来输入行。所以如果3,ListView将有3行Combo#(TextView)和C​​omboEdit(EditText)。

问题:我无法在ListView中设置TextView的文本。 我尝试按照此处的教程http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/进行操作。

:这主要是暂时的。假设我可以解决这个问题,想法是基于ListView中ComboEdit的输入,我想根据ListView中它旁边的ComboEdit输入生成另一个视图。所以,如果在ComboEdit中为4,我想生成一个复选框。然后在最后,每当单击Calculate Damage时,它将从视图中获取所有输入并将其传递到Java类以进行损坏计算。如果可能的话,我想在这里提出一些建议。

请原谅基本设计。如果我不尝试设置TextView文本,它会生成它。

Without setting Text

我对Android很陌生,所以我会很感激一些关于我是否正朝着正确的方向前进的指导。

XML

input_combos.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin">

<TextView
    android:id="@+id/enemy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/enemy"/>

<TextView
    android:id="@+id/enemyHP"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hp"
    android:layout_below="@+id/enemy"/>

<EditText
    android:id="@+id/enemyHPEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/enemy"
    android:layout_toLeftOf="@+id/enemyType"
    android:layout_toStartOf="@+id/enemyType"
    android:inputType="number"
    android:ems="5"/>

<TextView
    android:id="@+id/enemyDef"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/def"
    android:layout_below="@+id/enemyHPEdit"/>

<EditText
    android:id="@+id/enemyDefEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/enemyHPEdit"
    android:layout_toLeftOf="@+id/enemyType"
    android:layout_toStartOf="@+id/enemyType"
    android:inputType="number"
    android:ems="5"/>

<TextView
    android:id="@+id/enemyType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/type"
    android:layout_below="@+id/enemy"
    android:layout_centerHorizontal="true"/>


<Spinner
    android:id="@+id/enemyType1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/enemy">
</Spinner>

<Spinner
    android:id="@+id/enemyType2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/enemyType1">
</Spinner>

<Spinner
    android:id="@+id/enemyType3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/enemyType2">
</Spinner>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/num_combos"
    android:layout_below="@id/enemyType3"
    android:id="@+id/numCombos"/>

<EditText
    android:id="@+id/numCombosEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/numCombos"
    android:layout_toEndOf="@+id/numCombos"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_below="@id/enemyType3"
    android:inputType="number"
    android:ems="2"/>

<Button
    android:text="@string/calc_dmg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/calcDmg" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/numCombosEdit"
    android:layout_above="@id/calcDmg">

    <ListView
        android:id="@+id/combos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

</RelativeLayout>

input_combos_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="Combo: "
    android:id="@+id/combo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/comboEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="2"/>

</LinearLayout>

爪哇

InputCombos.java

public class InputCombos extends Activity {

private ArrayList<String> combosNum;

private int comboCount;

TextView enemy;
TextView enemyHP;
TextView enemyDef;
TextView enemyType;
TextView numCombos;

EditText enemyHPEdit;
EditText enemyDefEdit;
EditText numCombosEdit;

Spinner enemyType1;
Spinner enemyType2;
Spinner enemyType3;

ListView combos;

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

    enemy = (TextView) findViewById(R.id.enemy);
    enemyHP = (TextView) findViewById(R.id.enemyHP);
    enemyDef = (TextView) findViewById(R.id.enemyDef);
    enemyType = (TextView) findViewById(R.id.enemyType);

    enemyHPEdit = (EditText) findViewById(R.id.enemyHPEdit);
    enemyDefEdit = (EditText) findViewById(R.id.enemyDefEdit);

    enemyType1 = (Spinner) findViewById(R.id.enemyType1);
    enemyType2 = (Spinner) findViewById(R.id.enemyType2);
    enemyType3 = (Spinner) findViewById(R.id.enemyType3);

    numCombos = (TextView) findViewById(R.id.numCombos);
    numCombosEdit = (EditText) findViewById(R.id.numCombosEdit);
    numCombosEdit.addTextChangedListener(comboTextListener);

    combos = (ListView) findViewById(R.id.combos);
}

private TextWatcher comboTextListener = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(charSequence.length() != 0){
            comboCount = Integer.parseInt((charSequence).toString());
            combosNum = new ArrayList<>();
            for(int x = 0; x < comboCount; x++){
                combosNum.add("Combo " + Integer.toString(x+1));
            }
            System.out.println(combosNum);
            ComboAdapter comboAdapter = new ComboAdapter();
            combos.setAdapter(comboAdapter);
        }

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

private class ComboView{
    TextView combo;
    EditText comboEdit;
    int pos;
}

private class ComboAdapter extends BaseAdapter{


    // Returns the size of the generated array list
    @Override
    public int getCount() {
        if(combosNum != null && combosNum.size() != 0){
            return combosNum.size();
        }
        return 0;
    }

    // Gets the string from the array list and returns it
    @Override
    public Object getItem(int i) {
        return combosNum.get(i);
    }

    // Returns position
    @Override
    public long getItemId(int i) {
        return i;
    }

    // Returns the views to be placed in the list view
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ComboView comboView;
        if(view == null){
            comboView = new ComboView();
            LayoutInflater layoutInflater = InputCombos.this.getLayoutInflater();
            view = layoutInflater.inflate(R.layout.input_combos_view, null);
            comboView.combo = (TextView) findViewById(R.id.combo);
            comboView.comboEdit = (EditText) findViewById(R.id.comboEdit);

            view.setTag(comboView);
        } else {
            comboView = (ComboView) view.getTag();
        }

        comboView.pos = i;

        System.out.println(comboView.pos);

        System.out.println(combosNum);
        comboView.combo.setText(combosNum.get(i));

        return view;
    }
}


}

帮助。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                            at andycreuszheng.padcalculator.InputCombos$ComboAdapter.getView(InputCombos.java:158)
                                                                            at android.widget.AbsListView.obtainView(AbsListView.java:2344)
                                                                            at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
                                                                            at android.widget.ListView.onMeasure(ListView.java:1182)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
                                                                            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
                                                                            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
                                                                            at android.view.View.measure(View.java:17430)
                                                                            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
                                                                            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

0 个答案:

没有答案