2017-02-27 7 views
-2

GridViewメソッドを実装していました。すべてがうまくいって、コードごとに8つの画像のグリッドが表示されましたが、画像をクリックすると2つのボタンを持つダイアログが表示されるコードを追加しました。しかし、私は完全なコードを実行すると、アプリは、上記のキャプションとして問題を引き起こした。私はコードを含んでいます。欠けているものが見つかった場合は、以下にコメントしてください。それも貼り付けます。助けてください!残念ながら、* 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.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

logcat –

+0

@ GabrieleMariotti Doneを追加する必要があります。 – ShubhGOYAL

+0

あなたのAndroidManifest.xmlをtheme.xmlと共に表示してください。 – xklakoux

答えて

1

あなたがに必要AppCompatActivityTheme.AppCompatを使用して使用しているので(または子孫)あなたのアプリケーションまたはあなたの活動にそれを適用してください。以下のような

何か:マニフェストに

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

+0

ありがとう、トン! – ShubhGOYAL

0

は、あなたの活動のためのマニフェストでテーマを追加

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

以下のようなものは、この行は、例外が言う同じように、問題が発生します。使用

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

サポートライブラリをグラブファイルに追加する必要がある場合もあります。

関連する問題