程序无法打开,但没有编译错误

时间:2017-03-15 17:48:52

标签: android

你们好吗看看这个吗? MainActivity.java代码:

package com.mi47.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridView;

        gridView = (GridView)findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getId()==R.id.grid_view){
            switch(position){
                case 0:{
                    startActivity(new Intent(this,DotDraw.class));
                    break;
                }
                case 1:{
                    Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                    break;
                }
                case 2:{
                    Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                    break;
                }
                case 3:{
                    Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                    break;
                }
                case 4:{
                    Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                    break;
                }
                case 5:{
                    Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    Toast.makeText(MainActivity.this, "Click Inside" + position, Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    @Override
    public void onClick(View v) {

    }
}

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="3"
        android:verticalSpacing="72dp"
        android:horizontalSpacing="48dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>

编译时没有任何警告/错误。 DotDraw类只是一个新的页面即时开放,没什么重要的。知道为什么程序会一直关闭吗?

logcat的: 03-15 18:41:12.567 8655-8655 /? E / AndroidRuntime:致命异常:主要                                                  过程:com.mi47.test,PID:8655                                                  java.lang.RuntimeException:无法启动活动ComponentInfo {com.mi47.test / com.mi47.test.MainActivity}:java.lang.RuntimeException:不要为AdapterView调用setOnClickListener。您可能需要setOnItemClickListener                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                      在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                      在android.os.Looper.loop(Looper.java:154)                                                      在android.app.ActivityThread.main(ActivityThread.java:6119)                                                      at java.lang.reflect.Method.invoke(Native Method)                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                   引起:java.lang.RuntimeException:不要为AdapterView调用setOnClickListener。您可能需要setOnItemClickListener                                                      在android.widget.AdapterView.setOnClickListener(AdapterView.java:800)                                                      在com.mi47.test.MainActivity.onCreate(MainActivity.java:24)                                                      在android.app.Activity.performCreate(Activity.java:6679)                                                      在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                      在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)                                                      在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                      在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                      在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                      在android.os.Looper.loop(Looper.java:154)                                                      在android.app.ActivityThread.main(ActivityThread.java:6119)                                                      at java.lang.reflect.Method.invoke(Native Method)                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:1)

日志cat中明确提到了错误

Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead

您在OnClickListener中设置了GridView AdapterView。不允许此操作。

您应该使用OnItemClickListener代替,这将在GridView上单击特定项目时为您提供回调。

答案 1 :(得分:0)

gridView.setOnClickListener(this);

这是问题

您需要使用Gridview适配器:

gridView.setOnItemClickListener(this);