Android - 单击图像按钮时崩溃

时间:2014-09-08 19:09:16

标签: android crash click imagebutton android-imagebutton

当我点击我的图像按钮时,我的应用程序崩溃了。 它发生在模拟器和真实手机上。我在这里和一些其他论坛检查了我的代码,但是我找不到错误。我很高兴任何解决方案!

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日志

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

请帮忙。对不起英语不好,我来自捷克代表。谢谢

2 个答案:

答案 0 :(得分:0)

LogCat显示IllegalStateException。这里有一个类似的问题:

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

根据LogCat消息,您似乎需要更改主题XML文件中的主题以匹配所需的主题。

答案 1 :(得分:0)

线索在你的LogCat输出中:

引起:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

因此,对于从ActionBarActivity派生的HelpActivity,您需要使用Theme.AppCompat。

请在此处阅读 - https://developer.android.com/training/basics/actionbar/styling.html。 打开Android Manifest,找到HelpActivity,添加 机器人:主题=" @android:风格/ Theme.AppCompat"对于黑暗主题或" Theme.AppCompat.Light"对于轻的一个。