2014-10-26 46 views
0

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

下面是代码:

在MainActivity:

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> 

希望你能帮帮我!

感谢,

亚历

+0

你可以发布你的按钮的XML? – 2014-10-26 21:41:49

+0

我用XML更新了原始帖子 – user3407957 2014-10-26 21:44:24

回答

0

我相信我已经找到了你的错误。 在你MainActivity.class变化

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

Intent intent = new Intent(context, Crafting.class); 
startActivity(intent); 

,并添加下面的公共无效SetupCraftingButton()

final Context context = this; 

然后添加此

import android.content.Context; 

你进口

的名单。此外作为斯蒂芬(你可以接受他的这个回答,不是我的)指出,你有没有叫SetupCraftingButton()。

所以,你的代码看起来像这样

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

所以,你在你的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); 
      } 
     }); 

    } 
+0

我在单词上下文中出现错误。 – user3407957 2014-10-26 21:47:22

+0

我更新了代码,希望它能正常工作 – 2014-10-26 21:48:07

+0

我又更新了答案 – 2014-10-26 21:53:22

1

SetupCraftingButton()不会被调用。

setContentView(R.layout.button_test)之后的第一行onCreate() { ... }之后打电话给它,这应该可以解决您的问题。

+0

谢谢你解决了我的问题! – user3407957 2014-10-26 22:01:11

相关问题