Android Studio:初始化ImageButton时崩溃

Android Studio:初始化ImageButton时崩溃,android,image,button,crash,imagebutton,Android,Image,Button,Crash,Imagebutton,我试图通过单击imagebutton打开一个新活动。我放置了imagebutton并尝试为它创建一个onclick方法。应用程序已经在我试图找到我的按钮的地方崩溃了。它确实打印了第一个日志,但仅此而已。它甚至没有使用onClick方法。我对安卓系统非常陌生,所以我几乎没有使用安卓系统的经验 public class Bestellingen extends AppCompatActivity implements AdapterView.OnItemClickListener { pr

我试图通过单击imagebutton打开一个新活动。我放置了imagebutton并尝试为它创建一个onclick方法。应用程序已经在我试图找到我的按钮的地方崩溃了。它确实打印了第一个日志,但仅此而已。它甚至没有使用onClick方法。我对安卓系统非常陌生,所以我几乎没有使用安卓系统的经验

public class Bestellingen extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ArrayList<String> elementen;
    private ListView lV;
    private MenuAdapter mA;


    ImageView image;
    Spinner spinner1;
    ImageButton imageButtonWinkelmand;

    public Bestellingen() {
        Log.d("tag","test");
        imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);  // here
        Log.d("testje","erna");
}
我确实在activity_main.xml中创建了它

<ImageButton

    android:layout_width="80dp"
    android:layout_height="70dp"
    android:scaleType="fitXY"
    android:id="@+id/imageWinkelmand"
    android:src="@drawable/winkelmand"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="-20dp"
    android:background="#00000000"
    android:contentDescription="winkelmandje"/>

聊天对话结束

覆盖onCreate()并在onCreate()中查找视图

ImageButton imageButtonWinkelmand;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }
像这样使用。

覆盖onCreate()并在onCreate()中查找视图

ImageButton imageButtonWinkelmand;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }

像这样使用。

首先删除
Bestellingen
方法

然后在oncreate()部分中调用
ImageButton

imageButtonWinkelmand=(ImageButton)findViewById(R.id.imageWinkelmand)

最后

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        setContentView(R.layout.Your_layout_name);
        imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);

首先删除
Bestellingen
方法

然后在oncreate()部分中调用
ImageButton

imageButtonWinkelmand=(ImageButton)findViewById(R.id.imageWinkelmand)

最后

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        setContentView(R.layout.Your_layout_name);
        imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);

在这里发布日志。在oncreate部分调用它
imageButtonWinkelmand=(ImageButton)findViewById(R.id.imageWinkelmand)将其放入onCreate()中工作正常,谢谢!在这里发布日志。在oncreate部分调用它
imageButtonWinkelmand=(ImageButton)findViewById(R.id.imageWinkelmand)将其放入onCreate()中工作正常,谢谢!我还不能,需要等10分钟我还不能,需要等10分钟mins@LievenLuyckx很高兴听到。前进。@LievenLuyckx很高兴听到。前进。