2011-11-03 3 views
1

알림 바에서 클릭 한 후 열리는 활동에서 내 알림을 삭제하는 옵션을 제공하고 싶습니다. 나는 다음과 같은 코드를 사용하고 있습니다 :android에서 삭제 알림

nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     Log.i(TAG, "after NotificationManager"); 
     Intent intent = new Intent(this,test.class); 
     intent.putExtra("title", title); 
     intent.putExtra("subject", subject); 
     intent.putExtra("notificationid", count); 
     Log.i(TAG, title); 
     Log.i(TAG, subject); 
     PendingIntent pi = PendingIntent.getActivity(this, count, intent,0); 



     Notification n = new Notification(R.drawable.icon,subject,System.currentTimeMillis()); 

     n.setLatestEventInfo(this, title, subject, pi); 
     n.defaults = Notification.DEFAULT_LIGHTS; 

     nm.notify(count, n); 

을 그리고 내 test.java은 다음과 같습니다

public class test extends Activity { 


    TextView Title; 
    TextView Subject; 
    Button Clear; 
    protected int notificationid; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     String title=""; 
     String subject=""; 



     Bundle extras = getIntent().getExtras(); 

     if(extras !=null) { 
      title = extras.getString("title"); 
      subject = extras.getString("subject"); 
      notificationid = extras.getInt("notificationid"); 
     } 
     Title = (TextView) findViewById(R.id.Title); 
     Subject = (TextView) findViewById(R.id.Subject); 
     Title.setText(title); 
     Subject.setText(subject); 
     Clear.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) 
      {  

        NotificationManager nm ; 
       nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
       Log.i(getClass().getSimpleName(), " onClick inside test caled...."); 
       nm.cancel(notificationid); 


      } 
     }); 

    } 
} 

을하지만 응용 프로그램이 예기치 않게 종료지고 있습니다. 아무도 나를 도울 수 있습니까? 감사합니다.

+0

오류 로그를 게시하십시오. – user370305

답변

2

로그 캣 ;-)?

문제가되어야합니다. Button 구성 요소를 구문 분석하지 않았기 때문에 NullPointerException이 표시 될 수 있으며 직접 사용하려고합니다.

+0

예 ... 당신의 시간을 낭비하는 것에 대해 너무 유감입니다. 나는 이것을 잊어 버릴 수 있습니다. 감사합니다 ... – Ritu

+0

@Kartik, +1. XYZ, 카일의 제안을 따르고 LogCat을 설정했는지 확인하는 것이 좋습니다. 이렇게하면 미래의 문제를 훨씬 쉽게 찾을 수 있습니다 .-) –

+0

@aaamos : 예. 맞습니다. –

1

예기치 않은 코드와 관련된 오류가 무엇인가요? 충돌을 일으키는 예외에 대해 자세히 설명해 주시겠습니까?

LogCat을 사용합니까? 그렇지 않은 경우 http://developer.android.com/guide/developing/tools/logcat.html을 확인하십시오. 명령 프롬프트에서 logcat 로그를 직접 보려면 명령 프롬프트를 열고 android sdk가있는 디렉토리로 이동하십시오. 실행 adb logcat. logcat에 의해 생성 된 오류, 경고 등의 인쇄물을보기 시작해야합니다.

충돌의 원인을 파악할 수없는 경우 logcat (NullPointerException, ResourceNotFoundException 등) 및 문제의 원인이되는 코드 줄을 알려주며 문제가 무엇인지 확인할 수 있습니다. 그런 다음 여기에 오류 정보를 붙여 넣으십시오.

1

LogCat 출력을 볼 수 없으면 오류가 정확히 무엇인지 알 수 없지만 코드를 보면 (부분 코드 만 붙여 넣었다면 잘못 될 수 있음), 그렇지 않습니다. extras == null 인 경우 notificationid 변수를 초기화하는 중입니다. Eclipse로 개발 중이면 LogCat보기를보고 오류 스택 추적을 찾으십시오. 카일의 답변을보고 난 후에

편집 : 자신에 대해 말을 당신은 당신의 test 클래스에

Clear=(Button) findViewById(R.id.ClearButtonId); 

누락

0

는 FLAG_AUTO_CANCEL를 사용해보십시오 :

n.flags |= Notification.FLAG_AUTO_CANCEL; 

비트는이 사용자가 클릭 될 때 통지를 취소 할 필요가있는 경우 설정해야합니다 플래그 필드에 비트 단위의 논리합 할 수 있습니다.