gpt4 book ai didi

java - ListView 中数组适配器的空指针异常。安卓

转载 作者:搜寻专家 更新时间:2023-11-01 08:45:15 24 4
gpt4 key购买 nike

我有一个主视图(listview)和第二个主视图(edittext)。我使用 startActivityforResult() 将数据从第二个主视图向后传递到第一个主视图。我已经对其进行了测试,并且毫无问题地收到了字符串。但是,在 ListView 中显示它存在一些问题。我的 ListView 有两个 xml 文件,一个用于 ListView 布局,一个用于 TextView 行。我不知道为什么会出现空指针异常。

03-14 22:50:33.429    9268-9268/com.example.anusha.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.anusha.app, PID: 9268
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2780)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
at android.widget.ListView.onMeasure(ListView.java:1186)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
at android.view.View.measure(View.java:17594)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2308)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2030)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6640)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

这是我的 ListView 的第一个主视图。我很确定问题出在数组适配器上。虽然不知道它是什么。

 ArrayList<String> array= new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_address_list,R.id.rowTextView, array);
ListView listview = (ListView) findViewById(R.id.listView);
listview.setAdapter(adapter);



}
private void Add() {
Intent intent = new Intent(AddressList.this, SilentGeofence.class);
startActivityForResult(intent, 2);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (resultCode == RESULT_OK){
String result = data.getStringExtra("result");
array.add(result);


}
}
}

这是我的第二个主要

   Button add = (Button) findViewById(R.id.button);
add.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

String ag = txt1.getText().toString().trim();
txt1.setText("");
Intent returnIntent = new Intent();
returnIntent.putExtra("result",ag);
setResult(RESULT_OK,returnIntent);
finish();

最佳答案

public ArrayAdapter(上下文上下文,int 资源,int textViewResourceId,T[] 对象)

在 API 级别 1 中添加构造函数

参数context 当前上下文。resource 布局文件的资源 ID,其中包含实例化 View 时要使用的布局。textViewResourceId 要填充的布局资源中 TextView 的 idobjects 要在 ListView 中表示的对象。

所以你应该使用 textview 的布局而不是 listview 布局 see here see here

    new ArrayAdapter<>(this, R.layout.layout_name_of_the_xml_containing_textview,R.id.rowTextView, array);

关于java - ListView 中数组适配器的空指针异常。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29057777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com