【问题标题】:Android Studio: crash when initialising ImageButtonAndroid Studio:初始化 ImageButton 时崩溃
【发布时间】:2016-04-08 01:34:02
【问题描述】:

我正在尝试通过单击图像按钮来打开一个新活动。我放置了 imagebutton 并尝试为它创建一个 onclick 方法。该应用程序已经在我尝试找到我的按钮的地方崩溃了。它确实打印了第一个日志,但就是这样。它甚至没有到达 onClick 方法。我对 android 很陌生,所以我几乎没有使用它的经验。

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");
}

我的 onClick 方法

   imageButtonWinkelmand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("in de onclick","test");
                startActivity(new Intent(Bestellingen.this, BestellingenInfo.class));
            }
        });

我确实在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 部分调用这个imageButtonWinkelmand = (ImageButton) findViewById(R.id.imageWinkelmand);
  • 把它放在 onCreate() 工作,谢谢!

标签: android image button crash imagebutton


【解决方案1】:

首先删除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);

【讨论】:

  • @LievenLuyckx 很高兴听到。继续前进。
【解决方案2】:
ImageButton imageButton;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButton = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }

【讨论】:

    【解决方案3】:

    覆盖 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); 
    
        }
    

    这样使用。

    【讨论】:

    • 我还不行,需要等10分钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多