如何以编程方式使用View.setID(int id)在图像中设置OnClick

时间:2017-05-23 01:38:50

标签: java android android-layout

对不起,如果我的英语不是很好

我按照本教程https://www.youtube.com/watch?v=ChrWz8ZMyOU做了一个项目(记忆卡游戏) 我做的有点不同  我创建了一个显示游戏名称的menu_activity和一个带有播放图标的图像 此图标转到level_activity,此处我有三张图像指示难度

该项目没有显示任何错误,但它会运行,但此刻我转到Level_Activity并点击难度,应用程序无法执行任何操作。我对此并不了解,但我认为问题是目前实施OnClick。我尝试使用主菜单创建OnClick:

public void goGame(View view){
    Intent intent = new Intent(this, LevelActivity.class);
    startActivity(intent);

}
}

但是当我这样做的时候我点击了难度,应用程序关闭了 LogCat显示了这个:

05-22 20:17:20.578 12640-12640/com.mady.quimicamemoria E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.mady.quimicamemoria, PID: 12640
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mady.quimicamemoria/com.mady.quimicamemoria.EasyActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                         at com.mady.quimicamemoria.EasyActivity.onCreate(EasyActivity.java:39)
                                                                         at android.app.Activity.performCreate(Activity.java:6237)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我告诉你代码。

LevelActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class LevelActivity extends AppCompatActivity {


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

指示难度的图像的XML

<ImageView
            android:id="@+id/easylevel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/facil"
            android:layout_gravity="center"
             />

Class EasyActivity

public class EasyActivity extends AppCompatActivity implements 
View.OnClickListener { 

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

//我跳过了我认为不会影响问题的代码

for (int r = 0; r < numRows; r++){
        for(int c = 0; c < numColumns; c++){
            MemoryButton tempButton = new MemoryButton(this, r, c, 
buttonGraphics[buttonGraphicLocations[r * numColumns + c]]);
            tempButton.setId(View.generateViewId());
            tempButton.setOnClickListener(this);
            buttons[r * numColumns + c] = tempButton;
             gridLayout.addView(tempButton);
        }
    }
@Override
public void onClick(View view) {....}

在这种情况下,LogCat会显示:

05-22 20:04:00.791 6042-6103/com.mady.quimicamemoria E/Surface: 
getSlotFromBufferLocked: unknown buffer: 0xaa0d6380

0 个答案:

没有答案