2017-03-15 120 views
-1

你可以看看這個請嗎? 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:致命例外:main 進程:com.mi47.test,PID:8655 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.mi47.test/com.mi47.test.MainActivity}:java。 lang.RuntimeException:不要爲AdapterView調用setOnClickListener。你可能想要android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) android.app.ActivityThread.-wrap12(ActivityThread。)中你可能想setOnItemClickListener而不是 。 java) at android.app.ActivityThread $ h.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java: 154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArg sCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 引起:java.lang.RuntimeException:不要爲AdapterView調用setOnClickListener。您可能需要setOnItemClickListener而不是 at android.widget.AdapterView.setOnClickListener(AdapterView.java:800) at com.mi47.test.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity .java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java :2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit 。Java的:776)

+2

在崩潰期間發佈logcat錯誤 – rafsanahmad007

+0

將lgcat錯誤粘貼到此處並顯示dotdraw類謝謝 –

+0

請注意,編譯器不會(不能!)告訴您程序是否正確。它只能告訴你錯誤,使它很糟糕,無法處理它。 「它編譯」根本不是一個正確的指標。 – dsh

回答

1

的錯誤日誌貓明確提到,

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

要設置在GridView 0這是一個AdapterView。這個操作是不允許的。

你應該使用OnItemClickListener而不是那會給你回調時,在GridView上點擊一個特定的項目。

+0

是啊,我現在明白:(咖啡因是一種藥物的地獄....風聲鶴唳我打字錯誤的東西。 –

+0

哈哈,咖啡是生活,人,但你必須採取的數量 –

+0

,但畢竟照顧,你有沒有解決問題了嗎?@TahirIslam –

0

gridView.setOnClickListener(this);

這是你需要使用Gridview適配器問題

gridView.setOnItemClickListener(this); 
相關問題