本文介绍了应用程序崩溃-静音模式切换-傻瓜版Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了静音模式切换应用程序"(用于傻瓜的Android应用程序)的代码.我的代码与本书中的代码相同,但几乎没有例外,这是因为它具有较新的android.一切正常,但是如果我启动该应用程序,则会崩溃并显示错误消息:不幸的是,静默模式切换已停止.

I finished the code for the Silent Mode Toggle Application (android apps for dummies). My code is identical with the one in the book, with little exceptions because of the newer android. Everything works fine, but if I start the app, it crashes with the error message: Unfortunately, Silent Mode Toggle has stopped.

我发现问题出在setOnClickListener方法上.

I found out, that the problem is something with the setOnClickListener method.

几乎没有编译错误,eclipse表示一切都很不错.

There are no little compile errors, eclipse says everything is cool.

守则的相关部分:

 package com.dummies.android.silentmodetoggle;

 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.ActionBar;
 import android.support.v4.app.Fragment;
 import android.graphics.drawable.Drawable;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.os.Build;

 public class MainActivity extends ActionBarActivity {

private AudioManager mAudioManager;
private boolean mPhoneIsSilent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ////////////////////////////////////////////////////////////////////////////
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    /////////////////////////////////////////////////////////////////////////////

    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    checkIfPhoneIsSilent();
    setButtonClickListener();

}

    private void setButtonClickListener(){

    Button toggleButton = (Button) findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener (new View.OnClickListener() {

            public void onClick (View v){

            if (mPhoneIsSilent) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                mPhoneIsSilent=false;
            } else {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mPhoneIsSilent=true;
            }
 //             toggleUi();
        }
    });
}

    private void checkIfPhoneIsSilent(){
        int ringerMode = mAudioManager.getRingerMode();
        if (ringerMode == AudioManager.RINGER_MODE_SILENT){
            mPhoneIsSilent = true;
        } else {
            mPhoneIsSilent = false;
        }
    }

如果我用//注释掉setButtonClickListener方法的主体,以便它仅剩第一行:Button toggleButton =(Button)findViewById(R.id.toggleButton);该应用程序不会崩溃.因此,以某种方式setOnClickListener(带有继承的onClick)会带来麻烦.

If i comment the body of the setButtonClickListener method with // out, so that it has just the first line left: Button toggleButton = (Button) findViewById(R.id.toggleButton); than the application doesn't crash. So, somehow the setOnClickListener (with the inherited onClick) makes the trouble.

我检查了其他系统文件,但是如前所述,没有编译错误,我也检查了您的站点,但是我没有找到这个问题.所以我现在不知道.

I checked the other system files, but as mentioned, there are no compile errors, I checked your site too, but I haven't found this question. So I have no idea now.

我将不胜感激任何想法和帮助.抱歉,可能是非常业余的问题.非常感谢你!Z

I would appreciate any ideas and help. Sorry for the probably very amateur question.Thank you very much!Z

谢谢您的答复

您好,谢谢您的回复!

Android 4.4.2在清单中:

Android 4.4.2in the manifest:

我希望这是您提到的堆栈跟踪:

i hope this is the stack trace you mentioned:

04-07 12:58:32.560:D/AndroidRuntime(2370):关闭VM

04-07 12:58:32.560: D/AndroidRuntime(2370): Shutting down VM

04-07 12:58:32.560:W/dalvikvm(2370):threadid = 1:线程以未捕获的异常退出(group = 0xb2a7aba8)

04-07 12:58:32.560: W/dalvikvm(2370): threadid=1: thread exiting with uncaught exception (group=0xb2a7aba8)

04-07 12:58:32.580:E/AndroidRuntime(2370):致命异常:主要

04-07 12:58:32.580: E/AndroidRuntime(2370): FATAL EXCEPTION: main

04-07 12:58:32.580:E/AndroidRuntime(2370):进程:com.dummies.android.silentmodetoggle,PID:2370

04-07 12:58:32.580: E/AndroidRuntime(2370): Process: com.dummies.android.silentmodetoggle, PID: 2370

04-07 12:58:32.580:E/AndroidRuntime(2370):java.lang.RuntimeException:无法启动活动ComponentInfo {com.dummies.android.silentmodetoggle/com.dummies.android.silentmodetoggle.MainActivity}: java.lang.NullPointerException

04-07 12:58:32.580: E/AndroidRuntime(2370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dummies.android.silentmodetoggle/com.dummies.android.silentmodetoggle.MainActivity}: java.lang.NullPointerException

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

04-07 12:58:32.580:E/AndroidRuntime(2370):at android.app.ActivityThread.access $ 800(ActivityThread.java:135)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.access$800(ActivityThread.java:135)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.os.Handler.dispatchMessage(Handler.java:102)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.os.Handler.dispatchMessage(Handler.java:102)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.os.Looper.loop(Looper.java:136)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.os.Looper.loop(Looper.java:136)

04-07 12:58:32.580:E/AndroidRuntime(2370):在android.app.ActivityThread.main(ActivityThread.java:5017)

04-07 12:58:32.580: E/AndroidRuntime(2370): atandroid.app.ActivityThread.main(ActivityThread.java:5017)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于java.lang.reflect.Method.invokeNative(本机方法)

04-07 12:58:32.580: E/AndroidRuntime(2370): at java.lang.reflect.Method.invokeNative(Native Method)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于java.lang.reflect.Method.invoke(Method.java:515)

04-07 12:58:32.580: E/AndroidRuntime(2370): at java.lang.reflect.Method.invoke(Method.java:515)

04-07 12:58:32.580:E/AndroidRuntime(2370):在

04-07 12:58:32.580: E/AndroidRuntime(2370): at

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-07 12:58:32.580:E/AndroidRuntime(2370):在dalvik.system.NativeStart.main(本机方法)

04-07 12:58:32.580: E/AndroidRuntime(2370): at dalvik.system.NativeStart.main(Native Method)

04-07 12:58:32.580:E/AndroidRuntime(2370):由以下原因引起:java.lang.NullPointerException

04-07 12:58:32.580: E/AndroidRuntime(2370): Caused by: java.lang.NullPointerException

04-07 12:58:32.580:E/AndroidRuntime(2370):位于com.dummies.android.silentmodetoggle.MainActivity.setButtonClickListener(MainActivity.java:43)

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.dummies.android.silentmodetoggle.MainActivity.setButtonClickListener(MainActivity.java:43)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于com.dummies.android.silentmodetoggle.MainActivity.onCreate(MainActivity.java:36)

04-07 12:58:32.580: E/AndroidRuntime(2370): at com.dummies.android.silentmodetoggle.MainActivity.onCreate(MainActivity.java:36)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.Activity.performCreate(Activity.java:5231)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.Activity.performCreate(Activity.java:5231)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

04-07 12:58:32.580:E/AndroidRuntime(2370):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

04-07 12:58:32.580: E/AndroidRuntime(2370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

04-07 12:58:32.580:E/AndroidRuntime(2370):...还有11个

04-07 12:58:32.580: E/AndroidRuntime(2370): ... 11 more

推荐答案

我已经解决了这个问题(正好在笔记本电脑飞出我的窗户之前).

I have solved the problem (just in time before my laptop would have flown out of my window).

但是,解决方案还提出了其他一些问题.

However the solution made up some other questions.

所以首先,如果有人遇到相同的问题,我会这样做:

So first, if somebody has the same problem, i did this:

我已将扩展类更改为Activity.您必须将其导入.

I have changed the extended class to Activity. You will have to import it.

您将不得不删除onCreate方法中的'if'或将其注释掉.

You will have to delete or comment the 'if' in the onCreate method out.

并将setContentView的参数更改为:R.layout.fragment_main(我在fragment_main中具有ImageView和Button)

and change the parameter of the setContentView to: R.layout.fragment_main (i have the ImageView and the Button in the fragment_main)

嗯,它现在可以工作了,但这并不意味着我真正理解为什么以及问题所在,因为扩展ActionBarAcitivity"是默认设置,所以您希望它会工作.

Well, it works now, but it doesnt mean that I really undertsand why, and what was the problem, as the 'extends ActionBarAcitivity' is a default setting, you would expect that it will work.

但是我对此并不陌生,所以我不知道...

But I am new to this, so I have no clue...

我将在这个主题上做进一步的研究,但是如果你们能对此发表评论,是什么原因,现在到底发生了什么变化,等等,所以我将不胜感激!

I will do my research on this topic further, but if you guys could comment on this, what was the reason, what is exactly changed now, etc, I would really appreciate it!

谢谢

这篇关于应用程序崩溃-静音模式切换-傻瓜版Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:08