试图将图像按钮链接到Android类

试图将图像按钮链接到Android类,android,Android,请原谅,我是android编程新手。我试图将ImageButton链接到一个名为crafting的类,然后在该类中启动一个名为crafting的xml文件。该按钮在avd中似乎没有任何作用,但我正在努力查看错误所在 代码如下: 主要活动: package cf.angryman.app; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.vie

请原谅,我是android编程新手。我试图将ImageButton链接到一个名为crafting的类,然后在该类中启动一个名为crafting的xml文件。该按钮在avd中似乎没有任何作用,但我正在努力查看错误所在

代码如下:

主要活动:

package cf.angryman.app;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void SetupCraftingButton() {
        ImageButton craftingButton = (ImageButton) findViewById(R.id.crafting);
        craftingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), Crafting.class));
            }
        });

    }

}
工艺等级:

package cf.angryman.app;

import android.os.Bundle;
import android.app.Activity;

public class Crafting extends Activity {

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

}
以下是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/home_background" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_centerInParent="true" />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="341dp" >

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

                <ImageButton
                    android:id="@+id/crafting"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="30dp"
                    android:background="@null"
                    android:scaleType="fitXY"
                    android:src="@drawable/workbench" />

            </LinearLayout>

        </ScrollView>

    </LinearLayout>

希望你能帮忙

谢谢


亚历克斯我想我已经发现了你的错误。 在你的主要活动中,改变班级

startActivity(new Intent(getApplicationContext(), Crafting.class));

final Context context = this;
在您的公共void SetupCraftingButton()下方

然后加上这个

import android.content.Context;
添加到您的导入列表中

此外,正如Stephan(你可以接受他的答案,而不是我的答案)指出的,你没有调用SetupCraftingButton()

因此,您的代码将如下所示

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_test);
        SetupCraftingButton();
    }
public void SetupCraftingButton() {
        final Context context = this;
        ImageButton craftingButton = (ImageButton) findViewById(R.id.crafting);
        craftingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent(context, Crafting.class);
               startActivity(intent);
            }
        });

    }
因此,SetupCraftingButton()中的代码如下所示

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_test);
        SetupCraftingButton();
    }
public void SetupCraftingButton() {
        final Context context = this;
        ImageButton craftingButton = (ImageButton) findViewById(R.id.crafting);
        craftingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent(context, Crafting.class);
               startActivity(intent);
            }
        });

    }
SetupCraftingButton()
永远不会被调用


setContentView(R.layout.button_test)
之后的第一行
onCreate(){…}
中调用它,这应该可以解决您的问题。

您可以发布按钮的XML吗?我已经用XML更新了我的原始帖子按钮仍然不起作用,还有其他想法吗?谢谢您的帮助!谢谢你解决了我的问题!