不幸的是,*GridView* 已停止

Unfortunately, *GridView* has stopped

我正在实施 GridView 方法。一切都很顺利,按照我的代码显示了一个由八张图片组成的网格,但后来我添加了一段代码,点击一张图片会显示一些带有两个按钮的对话框。但是,当我执行完整代码时,应用程序出现了上述问题。我已经包含了代码,如果您发现有任何遗漏,请在下面评论,我也会粘贴。请帮忙!

Styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

AndroidManifest.xml:

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MyDialog"
        android:theme="@android:style/Theme.Dialog"
        android:label="ShubhApplication"></activity>
</application>

MyDialog.java:

package com.shubham.gridview;

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

public class MyDialog extends AppCompatActivity {

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

public void closeDialog(View v){

    finish();
}

public void closeDialog2(View v){

    finish();
}
}

MainActivity.java:

package com.shubham.gridview;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {


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

    myGrid = (GridView) findViewById(R.id.gridView);
    myGrid.setAdapter(new ShubhAdapter(this));

    myGrid.setOnItemClickListener(this);


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = new Intent(this, MyDialog.class);
    startActivity(intent);
}
}

class City
{
    int imageId;
String cityName;

City(int imageId, String cityName)
{
    this.imageId = imageId;
    this.cityName = cityName;

}
}

class ShubhAdapter extends BaseAdapter
{

ArrayList<City> list;
Context context;
ShubhAdapter(Context context)
{
    this.context = context;
    list = new ArrayList<City>();
    Resources res = context.getResources();
    String[] tempCityNames = res.getStringArray(R.array.city_names);
    int[] cityImages = { R.drawable.ahmedabad, R.drawable.bangalore, R.drawable.chennai,
                        R.drawable.delhi, R.drawable.hyderabad, R.drawable.kolkata,
                        R.drawable.lucknow, R.drawable.mumbai};

    for (int i=0; i<8; i++)
    {
        City tempCity = new City(cityImages[i], tempCityNames[i]);
        list.add(tempCity);
    }
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

class ViewHolder
{
    ImageView myCity;
    ViewHolder(View v)
    {
        myCity = (ImageView) v.findViewById(R.id.imageView);

    }
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {

    View row = convertView;
    ViewHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_item, parent, false);
        holder = new ViewHolder(row);
        row.setTag(holder);
    }

    else
    {
        holder = (ViewHolder) row.getTag();
    }

    City temp = list.get(i);
    holder.myCity.setImageResource(temp.imageId);

    return row;
}
}

logcat:

02-27 22:35:29.721 4972-4972/com.shubham.gridview E/AndroidRuntime: FATAL           EXCEPTION: main
                                                                Process: com.shubham.gridview, PID: 4972
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shubham.gridview/com.shubham.gridview.MyDialog}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
                                                                    at android.app.ActivityThread.access0(ActivityThread.java:151)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                    at android.os.Looper.loop(Looper.java:193)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5345)
                                                                    at java.lang.reflect.Method.invokeNative(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:515)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                    at dalvik.system.NativeStart.main(Native Method)
                                                                 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                                                                    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                    at com.shubham.gridview.MyDialog.onCreate(MyDialog.java:12)
                                                                    at android.app.Activity.performCreate(Activity.java:5343)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 
                                                                    at android.app.ActivityThread.access0(ActivityThread.java:151) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                    at android.os.Looper.loop(Looper.java:193) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5345) 
                                                                    at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                                                                    at dalvik.system.NativeStart.main(Native Method) 
02-27 22:40:29.756 4972-4972/com.shubham.gridview I/Process: Sending signal. PID: 4972 SIG: 9

由于您正在使用 AppCompatActivity 您需要 使用 Theme.AppCompat(或后代)并将其应用到您的应用程序或您的 Activity.

类似于:

<!-- styles.xml -->
<style name="AppTheme" parent="Theme.AppCompat">
  <!-- your app branding color for the app bar -->
  <item name="colorPrimary">@color/primary</item>
  <!-- darker variant for the status bar and contextual app bars -->
  <item name="colorPrimaryDark">@color/primary_dark</item>
  <!-- theme UI controls like checkboxes and text fields -->
  <item name="colorAccent">@color/accent</item>
 </style>

在清单中:

 <activity
    android:name=".MyDialog"
    android:theme="@style/AppTheme"></activity>

或:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

      ...

   </application>

Check this link 了解更多信息。

在清单中为您的 activity 添加一个主题,如下所示

<activity
        android:name=".MyDialog"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"></activity>
android:theme="@android:style/Theme.Dialog"

这一行会导致问题,就像异常所说的那样。使用

android:theme="@style/Theme.AppCompat.Light.Dialog"

您可能还需要将支持库添加到您的 gradle 文件中。