2013-04-21 5 views
1

나는 안드로이드에 매우 익숙합니다. & 지금 내 응용 프로그램에 이상한 문제가 있습니다. 그것은 콘솔에서 어떤 오류도주지 않습니다. 난 그냥 안드로이드의 다른 알림 옵션을 테스트하려고 여기 이런 이유로 내 작성된 코드입니다 : 여기안타깝게도 MyApp가 에뮬레이터에서 중지되었습니다.

class Home extends Activity { 

    Button notification; 
    Button progress; 
    private int ID=1; 
    ProgressDialog pd; 
    ProgressTask pt = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     notification=(Button)this.findViewById(R.id.start_notification); 
     notification.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       StartNotification(); 
      }}); 
     Button background = (Button)this.findViewById(R.id.change_background); 
     background.setOnClickListener(new OnClickListener() {  
      public void onClick(View v) { 
       showDialog(0); 

      }}); 
     progress.setOnClickListener(new OnClickListener() {  
      public void onClick(View v) { 
       showDialog(1); 

      }}); 
    } 
    @Override 
    protected Dialog onCreateDialog(int id) 
    { 
     final CharSequence[] items = {"Black","Red", "Green", "Blue", "Gray"}; 
     final String[] color = {"#202020","#D11919", "#339966", "#006699", "#818181"}; 
     switch(id) { 
     case 0: 
      return new AlertDialog.Builder(this) 

      .setTitle("Set Background Color") 
      .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int item) { 
        View mainLayout = findViewById(R.id.activity_home); 
        View root = mainLayout.getRootView(); 
        new Color(); 
        root.setBackgroundColor(Color.parseColor(color[item])); 
        dismissDialog(0); 
       } 
      }).create(); 

     case 1: 
      pd = new ProgressDialog(Home.this); 
      pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      pd.setMessage("Downloading..."); 
      return pd; 


     } 
     return null; 
    } 

    @Override 
    protected void onPrepareDialog(int id, Dialog dialog) {  
     switch(id) { 
     case 1: 
      pd.setProgress(0); 
      pt = new ProgressTask(handler); 
      pt.start();    
     } 

    } 

    final Handler handler = new Handler() { 
     public void handleMessage(Message msg) { 
      int count = msg.arg1; 
      pd.setProgress(count); 
      if (count >= 100){ 
       dismissDialog(1); 
       pt.setStatus(false); 
      } 
     } 
    }; 
    private class ProgressTask extends Thread { 
     Handler myHandler; 
     int count = 0; 
     boolean status = true; 

     ProgressTask(Handler _handler) { 
      myHandler = _handler; 
     } 

     public void run() {      

      while (status) { 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       Message msg = myHandler.obtainMessage(); 
       msg.arg1 = count; 
       myHandler.sendMessage(msg); 
       count++; 
      } 
     } 

     public void setStatus(boolean m){ 
      status = m; 
     } 
    } 

    private void StartNotification() 
    { 
     Intent intent= new Intent(Home.this, NotificationView.class); 
     intent.putExtra("ID", ID); 
     PendingIntent pendingIntent=PendingIntent.getActivity(Home.this, 0, intent, 0); 
     NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     Notification notif = new Notification(
       R.drawable.ball,"Reminder: Game starts in 15 minutes", 
       System.currentTimeMillis()); 
     CharSequence from = "Sports Center"; 
     CharSequence message = "Game: L.A Lakers vs Miami Heat"; 
     notif.setLatestEventInfo(this, from, message, pendingIntent); 

     notif.vibrate = new long[] { 100, 250, 100, 500}; 

     nm.notify(ID, notif); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_home, menu); 
     return true; 
    } 

} 

그리고 내 로그입니다 :

04-21 16:55:03.829: I/dalvikvm(907): threadid=3: reacting to signal 3 
04-21 16:55:03.966: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt' 
04-21 16:55:04.265: I/dalvikvm(907): threadid=3: reacting to signal 3 
04-21 16:55:04.545: D/dalvikvm(907): newInstance failed: Lcom/example/statusbar/Home; not accessible to Landroid/app/Instrumentation; 
04-21 16:55:04.545: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt' 
04-21 16:55:04.555: D/AndroidRuntime(907): Shutting down VM 
04-21 16:55:04.555: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
04-21 16:55:04.595: E/AndroidRuntime(907): FATAL EXCEPTION: main 
04-21 16:55:04.595: E/AndroidRuntime(907): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.statusbar/com.example.statusbar.Home}: java.lang.IllegalAccessException: access to class not allowed 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:137) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-21 16:55:04.595: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method) 
04-21 16:55:04.595: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:511) 
04-21 16:55:04.595: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-21 16:55:04.595: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-21 16:55:04.595: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method) 
04-21 16:55:04.595: E/AndroidRuntime(907): Caused by: java.lang.IllegalAccessException: access to class not allowed 
04-21 16:55:04.595: E/AndroidRuntime(907): at java.lang.Class.newInstanceImpl(Native Method) 
04-21 16:55:04.595: E/AndroidRuntime(907): at java.lang.Class.newInstance(Class.java:1319) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
04-21 16:55:04.595: E/AndroidRuntime(907): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 
04-21 16:55:04.595: E/AndroidRuntime(907): ... 11 more 
04-21 16:55:04.775: I/dalvikvm(907): threadid=3: reacting to signal 3 
04-21 16:55:04.826: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt' 
04-21 16:55:05.155: I/dalvikvm(907): threadid=3: reacting to signal 3 
04-21 16:55:05.175: I/dalvikvm(907): Wrote stack traces to '/data/anr/traces.txt' 
04-21 16:55:07.255: I/Process(907): Sending signal. PID: 907 SIG: 9 

어떤 문제가 될 수 있을까?

답변

0

코드에 두 가지 문제가 있습니다.

1- 클래스 public에 액세스 할 수 있도록하십시오. 2 - 당신은 당신이 nullPointer 지금 을 받고 왜 그

progress = (Button)this.findViewById(R.id.progress); 

를 추가하는 것을 잊었다, 당신은 갈 수 있습니다!

+0

감사합니다. @auicsc for point out. 진행 단추의 FindViewById를 포함하는 것을 잊었습니다. – user2304819

+1

[my * 2 * time old comment] (http://stackoverflow.com/questions/16133775/unfortunately-myapp-has-stopped-in-emulator/16133825#comment23047245_16133825)를 읽는 것이 너무 어려웠을 것입니다. 대답으로. –

5

는 스택 추적 말한다 이후 :

java.lang.IllegalAccessException: access to class not allowed 

그것은

class Home extends Activity { 

이 활동 달리 안드로이드는 할 수 없습니다 public 클래스해야

public class Home extends Activity { 

해야한다는 것을 의미 그들을 액세스하십시오.

+0

빠른 응답을 보내 주셔서 감사합니다. 나는 이미 그것을 시도했지만 운이 없다. 이제 java.lang.IllegalAccessException 대신 콘솔에 새 로그가 추가됩니다. 클래스에 대한 액세스가 허용되지 않습니다. "04-21 17 : 08 : 47.755 : E/AndroidRuntime (998) : java.lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {com.example.statusbar/com.example.statusbar.Home} : java.lang.NullPointerException" – user2304819

+2

안녕하세요. 다시 네가 반갑다. – Pragnani

+2

@ user2304819 첫 번째 문제는 해결되었지만 이제는 변수 중 하나가 'null'이고 액세스하려고하므로 NPE가 발생합니다. 'progress' 변수는'findViewById()'가 호출되지 않았기 때문에'null '인 것으로 보입니다. 더 많은 변수가있을 수 있습니다. 클래스가 다소 크기 때문에 디버그 문과 디버거를 사용하여 모든 것을 파악합니다. 그것은 배우는 중요한 기술입니다. –

관련 문제