-1

my app crash when i click my imagebutton. It happens in emulator and on real phone too. I checked my codes with some threads here and some other forum, but i cant find mistake. Im happy for any solution!

MainActivity.class

public class MainActivity extends Activity {

    public static final String PREFS_NAME = "MyPrefsFile";

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

        addListenerOnButton1();
        addListenerOnButton2();
        addListenerOnButton3();
        addListenerOnButton4();
        addListenerOnButton5();
        addListenerOnButton6();
        addListenerOnButton7();

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean dialogShown = settings.getBoolean("dialogShown", false);

        if (!dialogShown) {
          // AlertDialog code here
            new AlertDialog.Builder(this).setTitle(R.string.alter_title).setMessage(R.string.alter_content).setNeutralButton(R.string.alter_button, null).show();


          SharedPreferences.Editor editor = settings.edit();
          editor.putBoolean("dialogShown", true);
          editor.commit();    

        }
    }

    public void addListenerOnButton1() {

        ImageButton imgbutton1 = (ImageButton) findViewById(R.id.imageButton1);
        imgbutton1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "http://www.arcig.cz/bakalari/suplov.htm";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });

    }

    public void addListenerOnButton2() {

        ImageButton imgbutton2 = (ImageButton) findViewById(R.id.imageButton2);
        imgbutton2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "http://www.arcig.cz/bakalari/rozvrh.htm";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });
    }

    public void addListenerOnButton3() {

        ImageButton imgbutton3 = (ImageButton) findViewById(R.id.imageButton3);
        imgbutton3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "http://www.arcig.cz/bakaweb/login.aspx";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });
    }

    public void addListenerOnButton4() {

        ImageButton imgbutton4 = (ImageButton) findViewById(R.id.imageButton4);
        imgbutton4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "https://www.arcig.cz/roundcube/";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });
    }

    public void addListenerOnButton5() {

        ImageButton imgbutton5 = (ImageButton) findViewById(R.id.imageButton5);
        imgbutton5.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "http://www.arcig.cz/modules.php?op=modload&name=Web_Links&file=index&req=visit&lid=58";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });
    }

    public void addListenerOnButton6() {

        ImageButton imgbutton6 = (ImageButton) findViewById(R.id.imageButton6);
        imgbutton6.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                String url = "http://strav.nasejidelna.cz/0082/faces/login.jsp";

                Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class);
                myIntent.putExtra("url", url);
                startActivity(myIntent);
            }
        });
    }

    public void addListenerOnButton7() {

        ImageButton imgButton7 = (ImageButton) findViewById(R.id.imageButton7);
        imgButton7.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                Intent myIntent = new Intent(MainActivity.this, HelpActivity.class);
                MainActivity.this.startActivity(myIntent);

            }

        });

    }

}

HelpActivity.class

public class HelpActivity extends ActionBarActivity {

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

        addListenerOnButtonHelp();

    }

    public void addListenerOnButtonHelp() {

        Button buttonhelp = (Button) findViewById(R.id.buttonback);
        buttonhelp.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                Intent myIntent = new Intent(HelpActivity.this, MainActivity.class);
                HelpActivity.this.startActivity(myIntent);

            }

        });
    }

}

LogCat log

09-08 21:05:48.173: E/AndroidRuntime(9781): FATAL EXCEPTION: main
09-08 21:05:48.173: E/AndroidRuntime(9781): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tona.arcig/com.tona.arcig.HelpActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.os.Looper.loop(Looper.java:153)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread.main(ActivityThread.java:5297)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invoke(Method.java:511)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at dalvik.system.NativeStart.main(Native Method)
09-08 21:05:48.173: E/AndroidRuntime(9781): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at com.tona.arcig.HelpActivity.onCreate(HelpActivity.java:14)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.Activity.performCreate(Activity.java:5262)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
09-08 21:05:48.173: E/AndroidRuntime(9781):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
09-08 21:05:48.173: E/AndroidRuntime(9781):     ... 11 more
09-08 21:05:48.951: D/dalvikvm(9781): threadid=11: interp stack at 0x5ebab000

Please help. Sorry for bad english, im from czech rep. Thank you

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
antoninkriz
  • 966
  • 4
  • 18
  • 36

2 Answers2

0

LogCat is showing an IllegalStateException. There is a similar issue here:

ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat

It looks like you need to change the theme in your themes XML file to match the one required, as per the LogCat message.

Community
  • 1
  • 1
MattMatt
  • 2,242
  • 33
  • 30
0

The clue is in your LogCat output:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

So for the HelpActivity that is derived from ActionBarActivity you need to use a Theme.AppCompat.

Read here - https://developer.android.com/training/basics/actionbar/styling.html. Open your Android Manifest, find HelpActivity, add android:theme="@android:style/Theme.AppCompat" for dark theme or "Theme.AppCompat.Light" for light one.

Andrei
  • 139
  • 5
  • thanks bro! i used android:theme="@style/Theme.AppCompat.Light.DarkActionBar" and it looks nice! thank you again! – antoninkriz Sep 08 '14 at 19:54
  • no problem. Other than that your code is pretty much an example of inefficiency. Make all your URLs into a static array, write ONE method to launch a webview with the parameter URL. Setup one listener per activity. In the listener check what view is sending the click, locate the URL in the array and pass into the method. – Andrei Sep 08 '14 at 23:57
  • Iam new in java/andrid, so can you eyplain me that please? Thanks – antoninkriz Sep 09 '14 at 07:42
  • public class MainActivity extends Activity implements OnClickListener{ private static String myUrls = { "http://www.arcig.cz/bakalari/suplov.htm", "http://www.arcig.cz/bakalari/rozvrh.htm"} private void launchUrl(String url){ Intent myIntent = new Intent(MainActivity.this, WebviewActivity.class); myIntent.putExtra("url", url); startActivity(myIntent); } public void onClick(View v) { switch (v.getId()) { case R.id.R.id.imageButton1: launchUrl(myUrls[0]); break; case R.id.R.id.imageButton2: launchUrl(myUrls[1]); break; default: break; } } – Andrei Sep 12 '14 at 19:20