2017-02-27 3 views
-2

Ich implementierte GridView-Methode. Alles lief gut und zeigte ein Raster von acht Bildern nach meinem Code, aber dann fügte ich einen Code hinzu, der beim Klicken auf ein Bild einen Dialog mit zwei Knöpfen zeigte. Wenn ich jedoch den vollständigen Code ausführte, verursachte die App ein Problem wie oben beschrieben. Ich habe den Code eingefügt und wenn irgendetwas fehlt, bitte unten kommentieren, ich werde das auch einfügen. Bitte helfen Sie!Leider * GridView * wurde gestoppt

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.access$900(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.access$900(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 
+1

Sie sollten den Logcat –

+0

@Gabrielemarootti Done hinzufügen. – ShubhGOYAL

+0

zeigen Sie bitte Ihre AndroidManifest.xml, zusammen mit theme.xml – xklakoux

Antwort

1

Da Sie ein AppCompatActivitySie müssen verwenden, um eine Theme.AppCompat (oder Nachkommen verwenden) und wenden Sie es auf Ihre Anwendung oder Ihre Aktivität an.

Etwas wie:

<!-- 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> 

Im Manifest:

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

oder:

<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 für weitere Informationen.

+0

Danke eine Tonne ..! – ShubhGOYAL

0

ein Thema im Manifest für Ihre Tätigkeit etwas hinzufügen wie unten

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

Diese Linie Probleme verursacht, so wie die Ausnahme sagt. Verwenden Sie

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

Sie müssen möglicherweise auch Unterstützung Bibliothek zu Ihrer Gradle-Datei hinzufügen.