如何解决错误&QUOT

如何解决错误&QUOT

本文介绍了如何解决错误"无法启动活动ComponentInfo"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了code代表特定时间报警电话,而且时间后报警停止。我曾尝试在运行该模拟器code和一切顺利。但是,当我想这对我的HTC Desire HD(Android版2.3.5),应用程序获取力关闭。可有人告诉我发生了什么事?

 公共类RingerActivity延伸活动{
/ **当第一次创建活动调用。 * /
按钮preSS;
布尔音=真实的;
MediaPlayer的熔点;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    熔点为新的MediaPlayer();
    MP = MediaPlayer.create(RingerActivity.this,R.raw.alarm);
    尝试{
        MP prepare()。    }赶上(IllegalStateException异常五){
        e.printStackTrace();
    }赶上(例外五){
        Toast.makeText(这一点,e.getMessage(),Toast.LENGTH_SHORT).show(); }
    mp.start();    处理程序H =新的处理程序();
    可运行stopPlaybackRun =新的Runnable(){
        公共无效的run(){
            mp.stop();
            mp.release();
        }
    };
    h.postDelayed(stopPlaybackRun,20 * 1000);    AudioManager经理=(AudioManager)getSystemService(AUDIO_SERVICE);
    manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    INT maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){    如果(键code == KeyEvent.KEY code_VOLUME_UP){
        Toast.makeText(这一点,增加音量,Toast.LENGTH_LONG).show();
        返回true;
    }    如果(键code == KeyEvent.KEY code_VOLUME_DOWN){
        Toast.makeText(这一点,降低音量,Toast.LENGTH_LONG).show();
        返回true;
    }    返回super.onKeyDown(键code,事件);
}

logcat的

  11-09 13:36:51.220:E / AndroidRuntime(2370):致命异常:主要
11-09 13:36:51.220:E / AndroidRuntime(2370):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.testing.ringer / com.testing.ringer.RingerActivity}:显示java.lang.NullPointerException
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread.access $ 1500(ActivityThread.java:135)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.os.Looper.loop(Looper.java:150)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread.main(ActivityThread.java:4385)
11-09 13:36:51.220:E / AndroidRuntime(2370):在java.lang.reflect.Method.invokeNative(本机方法)
11-09 13:36:51.220:E / AndroidRuntime(2370):在java.lang.reflect.Method.invoke(Method.java:507)
11-09 13:36:51.220:E / AndroidRuntime(2370):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849)
11-09 13:36:51.220:E / AndroidRuntime(2370):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-09 13:36:51.220:E / AndroidRuntime(2370):在dalvik.system.NativeStart.main(本机方法)
11-09 13:36:51.220:E / AndroidRuntime(2370):因:显示java.lang.NullPointerException
11-09 13:36:51.220:E / AndroidRuntime(2370):在com.testing.ringer.RingerActivity.onCreate(RingerActivity.java:35)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-09 13:36:51.220:E / AndroidRuntime(2370):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
11-09 13:36:51.220:E / AndroidRuntime(2370):11 ...更多

清单

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.testing.ringer
安卓版code =1
机器人:=的versionName1.0><采用-SDK安卓的minSdkVersion =10/><使用许可权的android:NAME =android.permission.RECORD_AUDIO/><应用
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME>
    <活动
        机器人:名字=。RingerActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>< /清单>


解决方案

这个问题是由于在一个的onCreate异常空指针。 Somtimes音频服务未返回AudioManager。

更改code:

  AudioManager经理=(AudioManager)getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
INT maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);

如下面的onCreate:

  AudioManager经理=(AudioManager)getSystemService(AUDIO_SERVICE);
如果(经理!= NULL){
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
INT maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);
}

I have written a code for alarming phone for a particular time, and the alarm will stop after that period. I have tried running this code in emulator, and everything runs smoothly. But when I tried this to my HTC Desire HD (Android 2.3.5), the application gets force closed. Can someone tells me what happened???

public class RingerActivity extends Activity{
/** Called when the activity is first created. */
Button press;
boolean tone = true;
MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = new MediaPlayer();
    mp = MediaPlayer.create(RingerActivity.this, R.raw.alarm);
    try {
        mp.prepare();

    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        Toast.makeText(this, e.getMessage() , Toast.LENGTH_SHORT).show();         }
    mp.start();

    Handler h = new Handler();
    Runnable stopPlaybackRun = new Runnable() {
        public void run(){
            mp.stop();
            mp.release();
        }
    };
    h.postDelayed(stopPlaybackRun, 20 * 1000);

    AudioManager manager = (AudioManager)getSystemService(AUDIO_SERVICE);
    manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        Toast.makeText(this, "Volume Up", Toast.LENGTH_LONG).show();
        return true;
    }

    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
        Toast.makeText(this, "Volume Down", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Logcat

11-09 13:36:51.220: E/AndroidRuntime(2370): FATAL EXCEPTION: main
11-09 13:36:51.220: E/AndroidRuntime(2370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.ringer/com.testing.ringer.RingerActivity}: java.lang.NullPointerException
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.os.Looper.loop(Looper.java:150)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread.main(ActivityThread.java:4385)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at java.lang.reflect.Method.invoke(Method.java:507)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at dalvik.system.NativeStart.main(Native Method)
11-09 13:36:51.220: E/AndroidRuntime(2370): Caused by: java.lang.NullPointerException
11-09 13:36:51.220: E/AndroidRuntime(2370):     at com.testing.ringer.RingerActivity.onCreate(RingerActivity.java:35)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-09 13:36:51.220: E/AndroidRuntime(2370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
11-09 13:36:51.220: E/AndroidRuntime(2370):     ... 11 more

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testing.ringer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".RingerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
解决方案

The issue is due to a nullpointer exception at onCreate. Somtimes the Audio service is not returning the AudioManager.

Change the code :

AudioManager manager = (AudioManager)getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);

as below in onCreate :

AudioManager manager = (AudioManager)getSystemService(AUDIO_SERVICE);
if(manager!=null){
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
}

这篇关于如何解决错误&QUOT;无法启动活动ComponentInfo&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:47