NullPointerException setOnClickListner

时间:2016-05-05 15:38:40

标签: java android

我是android的初学者。 这是我从书中复制的代码。但是当我在智能手机上下载并运行时,会发生NullPointerException。

例外信息如下:

java.lang.NullPointerExcption:尝试在空对象引用上调用虚方法'void android.widget.Button.setOnClickListner ....

这是我的代码的一部分:

public class MainActivity extends AppCompatActivity {

    Button btn1 , btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       // requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setProgressBarIndeterminateVisibility(true);
                setProgressBarVisibility(true);
                setProgress(4500);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setProgressBarIndeterminateVisibility(false);
                setProgressBarVisibility(false);
            }
        });
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.neoncowboy.titleprogressbar.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示"
        android:id="@+id/btn1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐藏"
        android:id="@+id/btn2" />

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

在添加点击监听之前,您需要std::vector<T>

setContentView(R.layout.activity_main);

答案 1 :(得分:2)

setContentView()行必须在findViewById()行之前。

答案 2 :(得分:0)

您应该在此行之后使用imageView.bounds.width 将xml布局连接到您的活动类:setContentView(R.layout.yourXmlFileName);