android java onClick无法执行活动的方法

sve*_*vdz 13 java xml android android-recyclerview

这是我第一次使用recyclerview,我在android:onClick="addItem"这里得到一些错误,这是当我尝试向我的recyclerview添加一行文本时得到的.我经常使用手机测试我的应用程序.

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4012)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            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:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            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:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
            at com.sapps.app.testapp2.MainActivity.addItem(MainActivity.java:53)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            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:5312)
            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:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我认为是错误,但我不确切知道:

 public class MainActivity extends ActionBarActivity {

private EditText mText;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView recyclerView;
private Button btn;
private CustomRecyclerAdapter mAdapter;

private List<Data> mData = new ArrayList<>();

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

    // Initializing views.
    mText = (EditText) findViewById(R.id.textEt);
    recyclerView = (RecyclerView) findViewById(R.id.recycler);

    // If the size of views will not change as the data changes.
    recyclerView.setHasFixedSize(true);

    // Setting the LayoutManager.
    mLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(mLayoutManager);

    // Setting the adapter.
    CustomRecyclerAdapter mAdapter = new CustomRecyclerAdapter();
    recyclerView.setAdapter(mAdapter);
}



// Called when add button is clicked.
public void addItem(View v) {

    if(mText!=null) {
        Data dataToAdd = new Data(mText.getText().toString()); mData.add(dataToAdd);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的recyclerview适配器,以确定:

public class CustomRecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

    CustomRecyclerAdapter mAdapter;

    private List<Data> mData = Collections.emptyList();

    public CustomRecyclerAdapter() {
        // Pass context or other static stuff that will be needed.
    }

    public void updateList(List<Data> data) {
        mData = data;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.list_item, viewGroup, false);
        return new RecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) {
        viewHolder.title.setText(mData.get(position).text);
    }

    public void addItemInRec(int position, Data data) {
        mData.add(data);
        notifyItemInserted(position);
    }

    public void removeItem(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的MainActivity xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/textEt"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Text"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add"
            android:onClick="addItem"/>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

也许是我的ViewHolder:

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    public TextView title;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者Data.java类:

public class Data {

        public String text;
        public Data(String text) {
            this.text = text;
        }
    }
Run Code Online (Sandbox Code Playgroud)

med*_*eda 5

你正在getText()调用一个空指针

它表示以下行:

EditText mText = (EditText) findViewById(R.id.textEt);
Run Code Online (Sandbox Code Playgroud)

返回null,解决方案是检查并更正布局,以便textEt在其上.

编辑:

如果您确定它在布局中删除EditText声明.

声明为private EditText mText;类范围

setContentView(R.layout.name_of_layout_here);
mText = (EditText) findViewById(R.id.textEt);
Run Code Online (Sandbox Code Playgroud)


Kio*_*tch 5

关键在您的错误消息中:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
            at com.sapps.app.testapp2.MainActivity.addItem(MainActivity.java:53)
Run Code Online (Sandbox Code Playgroud)

从错误输出中学习的一个很好的技巧是查看每个“ Caused by”语句,并浏览日志,直到找到自己引用的文件之一。然后,错误日志的该部分甚至会告诉您代码失败的行(在本例中为MainActivity.java的第53行)。

Java中的空指针异常是当您尝试在某个对象'A'上调用方法而该对象'A'当前为时null

因此,此错误消息的意思是:“在MainActivity.java的第53行上,您试图在某个尚不存在的对象上调用方法,因此我崩溃了。”

失败的方法是 EditText mText = (EditText) findViewById(R.id.textEt);

通常,这种类型的故障意味着您无法从布局中找到正确的ID。仔细检查textEt是否为此布局元素的正确ID。

编辑:

仍然不确定为什么没有填充您的视图,但是我确实注意到适配器存在错误。您正在重新定义mAdapter,所以您有2个副本,一个在本地范围内,一个作为MainActivity的成员。这肯定会搞砸。

就在这儿:

// Setting the adapter.
CustomRecyclerAdapter mAdapter = new CustomRecyclerAdapter();
recyclerView.setAdapter(mAdapter)
Run Code Online (Sandbox Code Playgroud)

您正在mAdapter本地重新定义。改为这样做:

// Setting the adapter.
mAdapter = new CustomRecyclerAdapter();
recyclerView.setAdapter(mAdapter)
Run Code Online (Sandbox Code Playgroud)