简体   繁体   English

在GridView中设置EditText返回NullPointerException吗?

[英]Setting up EditText in a GridView returns NullPointerException?

Hello and happy holidays, everyone. 大家好,节日快乐。

This is the problem I'm having: I set up a GridView using mrKlar's PagedDragDrop here: https://github.com/mrKlar/PagedDragDropGrid 这是我遇到的问题:我在这里使用mrKlar的PagedDragDrop设置了GridView: https : //github.com/mrKlar/PagedDragDropGrid

I'm trying to make it so that instead of pre-determined text underneath each item, the user has the option to input text below it. 我正在尝试这样做,以便用户可以选择在其下方输入文本,而不是在每个项目下面使用预定文本。 To do this, my initial thought was to edit the Item.java file to make it so that it outputted an EditText instead of string. 为此,我最初的想法是编辑Item.java文件,以使其输出一个EditText而不是字符串。 So this is what I ended up with (you can compare it to the original Item.java file in the github link above): 所以这就是我的最终目的(您可以将其与上面的github链接中的原始Item.java文件进行比较):

package com.example.startingeleven;

import android.widget.EditText;

public class Item {

    private long id;
    private EditText name;
    private int drawable;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name.getText().toString();
    }
    public void setName(EditText name) {
        this.name = name;
    }
    public int getDrawable() {
        return drawable;
    }
    public void setDrawable(int drawable) {
        this.drawable = drawable;
    }
    public Item(long id, EditText name, int drawable) {
        super();
        this.id = id;
        this.name = name;
        this.drawable = drawable;
    }
}

This is the area of code in the main ExamplePagedDragDropGridAdapter file that I think is relevant to this 这是我认为与此相关的主要ExamplePagedDragDropGridAdapter文件中的代码区域。

public class ExamplePagedDragDropGridAdapter extends Activity implements PagedDragDropGridAdapter {

    private Context context;
    private PagedDragDropGrid gridview;

    List<Page> pages = new ArrayList<Page>();

    public ExamplePagedDragDropGridAdapter(Context context, PagedDragDropGrid gridview) {
        super();
        this.context = context;
        this.gridview = gridview;
        EditText striker1 = (EditText)findViewById(R.id.striker1);

        Page page1 = new Page();
        List<Item> items = new ArrayList<Item>();
        items.add(new Item(1, striker1, R.drawable.jersey));
        page1.setItems(items);
        pages.add(page1);
    }

This is the LogCat that displays the error: 这是LogCat,显示错误:

12-23 23:55:10.315: E/AndroidRuntime(1622): FATAL EXCEPTION: main
12-23 23:55:10.315: E/AndroidRuntime(1622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.startingeleven/com.example.startingeleven.FieldActivity}: java.lang.NullPointerException
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.os.Looper.loop(Looper.java:137)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at java.lang.reflect.Method.invoke(Method.java:525)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at dalvik.system.NativeStart.main(Native Method)
12-23 23:55:10.315: E/AndroidRuntime(1622): Caused by: java.lang.NullPointerException
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.Activity.findViewById(Activity.java:1853)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at com.example.startingeleven.ExamplePagedDragDropGridAdapter.<init>(ExamplePagedDragDropGridAdapter.java:63)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at com.example.startingeleven.FieldActivity.onCreate(FieldActivity.java:29)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.Activity.performCreate(Activity.java:5133)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-23 23:55:10.315: E/AndroidRuntime(1622):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-23 23:55:10.315: E/AndroidRuntime(1622):     ... 11 more
12-23 23:55:46.794: I/Process(1622): Sending signal. PID: 1622 SIG: 9

Am I on the right track editing the Item file to get an EditText to show up below the items? 我是否在编辑项目文件的正确轨道上,使EditText显示在项目下方? I have a feeling this might be a lot to ask for a solution for, but then again I'm not the most knowledgable in android programming lol. 我感觉这可能需要很多解决方案,但是再说一次,我并不是android编程中最了解的语言。 Either way, any fix to this problem is well appreciated, and please just ask if you require any more information. 无论哪种方式,都可以很好地解决此问题,请问您是否需要更多信息。

Thanks. 谢谢。

 EditText striker1 = (EditText)findViewById(R.id.striker1);

You are not inflating a layout. 您没有在扩大布局。 findViewById looks for a view with the id in the current infalted layout. findViewById在当前infalted布局中查找具有ID的视图。 Since you do not inflate a layout and try to initialize edittext you get NullPointerException 由于您不膨胀布局并尝试初始化edittext,因此会得到NullPointerException

You just need to first inflate the layout in which you defined your edittext ie striker1 and then from that layout you have to call findviewbyid and you will get the edittext instance..! 您只需要首先在其中定义了您的edittext的布局(即strider1)中进行充气,然后从该布局中调用findviewbyid即可获得edittext实例。

This is what you really need to do:-- 这是您真正需要做的:

 View layout = inflater.inflate(R.layout.your_xml_layout,
                        null);
 EditText striker1 = (EditText)layout.findViewById(R.id.striker1);

NOTE 注意

here R.layout.your_xml_layout : is the layout file in which you defined your edittext with id ie striker1. 在这里R.layout.your_xml_layout:是一个布局文件,您在其中定义了ID为id的edittext,即strike1。

just try it..! 去尝试一下..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM