Java 安卓:Can';t初始化TextView的数组
我有一只非常疯狂的虫子。当我创建一个TextView数组并使用xml中的id在onCreate中初始化它们时,没有任何更改。我创建了一个简单的函数来更改任何内容,但得到了一个nullpointer异常。我不知道我的代码有什么问题 我将非常感激任何帮助 XML:Java 安卓:Can';t初始化TextView的数组,java,android,arrays,Java,Android,Arrays,我有一只非常疯狂的虫子。当我创建一个TextView数组并使用xml中的id在onCreate中初始化它们时,没有任何更改。我创建了一个简单的函数来更改任何内容,但得到了一个nullpointer异常。我不知道我的代码有什么问题 我将非常感激任何帮助 XML: RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android" android:layout\u width=“匹配父项” android:layout\
RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:layout_weight=“10”
>
在函数change
中,日期[0]的值是多少?您是否尝试过在ArrayList中执行此操作,在ArrayList中初始化ArrayList,ArrayList dates=new ArrayList();?我很确定你现在的设置方式会给出一个npe,因为日期没有初始化。您甚至可以尝试将其更改为TextView[]dates=newtextView[10];或者别的什么。不是100%,只是看起来像一个possibility@user3294396我得到一个java.lang.NullPointerException,将其更改为getText()而不是getEditableText()。。。dates.get(i).getText().toString().compareTo(“”==0问题是您需要初始化私有TextView[]日期代码>你需要一个构造函数
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
>
<LinearLayout
android:id="@+id/keys"
.....
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/keys"
android:weightSum="6" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="@+id/data1" android:layout_alignTop="@+id/linearLayout"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="@+id/data2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="@+id/data3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="@+id/data4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:hint="A"
android:layout_weight="1"
android:id="@+id/data5"/>
</LinearLayout>
</RelativeLayout>
public class XXX extends Activity {
private String login;
....
private TextView[] dates;
public void change(String x) {
if(dates[0].getEditableText().toString().compareTo("") == 0)
dates[0].setText(x);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ABC);
dates = new TextView[5];
dates[0]=(TextView)findViewById(R.id.data1);
dates[1]= (TextView) findViewById(R.id.data2);
dates[2]=(TextView)findViewById(R.id.data3);
dates[3]=(TextView)findViewById(R.id.data4);
dates[4]=(TextView)findViewById(R.id.data5);
dates[0].setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
for (int i= 0; i < 5; i++){
dates[i].setText("LOL");
};
change("ROTFL");
}