数组适配器中的未知空指针异常

时间:2016-03-08 14:07:56

标签: android nullpointerexception android-arrayadapter

我在下面的行中得到一个空指针异常

ArrayAdapter<String> arrayadapt = new ArrayAdapter<String>(Loginpage.this,R.layout.activity_loginpage,R.id.textView1, numbers);

以下是代码,

public class Loginpage extends Activity implements OnItemClickListener{

    TextView txtview;
    ListView listview;
    String[] numbers = {"987654321","456789321","451236987","4178522369"};

    Intent newInt;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginpage);
        txtview = (TextView)findViewById(R.id.textView1);
        listview = (ListView)findViewById(R.id.listView1);
        ArrayAdapter<String> arrayadapt = new ArrayAdapter<String>(Loginpage.this,R.layout.activity_loginpage,R.id.textView1, numbers);
        listview.setAdapter(arrayadapt);
        listview.setOnItemClickListener(this);


    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String getdata = (String) parent.getItemAtPosition(position);

        newInt = new Intent(Intent.ACTION_DIAL);
        newInt.setData(Uri.parse("tel:"+getdata));
        startActivity(newInt);


    }


}

这是从另一个活动调用的,调用是来自这行代码,

Intent yIntent = new Intent(this, Loginpage.class);
            startActivity(yIntent);

来自 Logcat

03-08 08:35:52.613: E/AndroidRuntime(2060): FATAL EXCEPTION: main
03-08 08:35:52.613: E/AndroidRuntime(2060): Process: com.example.androidex2, PID: 2060
03-08 08:35:52.613: E/AndroidRuntime(2060): java.lang.NullPointerException
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.AbsListView.obtainView(AbsListView.java:2255)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.ListView.onMeasure(ListView.java:1175)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.View.measure(View.java:16497)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.View.measure(View.java:16497)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.View.measure(View.java:16497)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.View.measure(View.java:16497)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.View.measure(View.java:16497)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.Choreographer.doFrame(Choreographer.java:544)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.os.Handler.handleCallback(Handler.java:733)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.os.Looper.loop(Looper.java:136)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at android.app.ActivityThread.main(ActivityThread.java:5001)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at java.lang.reflect.Method.invoke(Method.java:515)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
03-08 08:35:52.613: E/AndroidRuntime(2060):     at dalvik.system.NativeStart.main(Native Method)

R.layout.activity_loginpage

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidex2.Loginpage" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    </ListView>

</LinearLayout>

4 个答案:

答案 0 :(得分:1)

您设置为Adapter主要布局 - R.layout.activity_loginpage

但是作为第二个参数,你必须传递一个View,它将在ListView中用作“行”

因此,使用一个listview_item.xml

创建一个新的xml布局“TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

立即致电

ArrayAdapter<String> arrayadapt = new ArrayAdapter<String>(Loginpage.this, R.layout.listview_item,R.id.textView1, numbers);

答案 1 :(得分:0)

您将错误的布局传递给适配器。 它应该是行的布局。

Javadoc The resource ID for a layout file containing a layout to use when instantiating views.

答案 2 :(得分:0)

不要将R.layout.activity_loginpage传递给ArrayAdapter的构造函数。此位置的参数是布局为ArrayAdapter呈现单个项。由于您提供的R.layout.activity_loginpage布局,NullPointer不包含ID为R.id.textView1的TextView。

要解决此问题,请创建一个单独的布局,用于定义标识为R.id.textView1的TextView,或者只使用默认布局之一:

ArrayAdapter<String> arrayadapt = new ArrayAdapter<String>(Loginpage.this,android.R.layout.simple_list_item_1, R.id.textView1, numbers);

答案 3 :(得分:0)

您没有提供id为textView1的文本视图。因此,在布局activity_loginpage中创建一个id为textView1的TextView。