2014-10-26 47 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

相關問題