开发者

Issue with custom Dialog Android

开发者 https://www.devze.com 2022-12-30 16:40 出处:网络
I have a custom Dialog in my app and I have a problem achieving the desired behavior. My Dialog has 4 Buttons. (Back, Valid, Modify and Restore). When the user clicks on Modify or Valid I would like t

I have a custom Dialog in my app and I have a problem achieving the desired behavior. My Dialog has 4 Buttons. (Back, Valid, Modify and Restore). When the user clicks on Modify or Valid I would like to call another activity. So I am using Intent but it's crashing.

The error Log :

05-19 13:29:21.495: ERROR/DEBUGTAG(974): java.lang.NullPointerException 05-19 13:29:21.495: ERROR/DEBUGTAG(974):

at android.content.ComponentName.(ComponentName.java:75) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.content.Intent.(Intent.java:2551) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.booztermobile.activity.HeaderMailDisplayActivity.onClick(HeaderMailDisplayActivity.java:571) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.View.per开发者_运维知识库formClick(View.java:2364) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.View.onTouchEvent(View.java:4179) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.widget.TextView.onTouchEvent(TextView.java:6540) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.View.dispatchTouchEvent(View.java:3709) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.app.Dialog.dispatchTouchEvent(Dialog.java:643) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.os.Handler.dispatchMessage(Handler.java:99) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.os.Looper.loop(Looper.java:123) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at android.app.ActivityThread.main(ActivityThread.java:4363) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at java.lang.reflect.Method.invokeNative(Native Method) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at java.lang.reflect.Method.invoke(Method.java:521) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 05-19 13:29:21.495: ERROR/DEBUGTAG(974): at dalvik.system.NativeStart.main(Native Method)

My custom Dialog :

package com.android.booztermobile.services;

import com.android.booztermobile.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;


public class MailDialog extends Dialog {

    private Button btnValid;
    private Button btnBack;
    private Button btnRestore;
    private Button btnModify;
    private Context context;


    public MailDialog(Context cont) {
        super(cont);
        context = cont;
        }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TestApp", "Dialog created");
            setContentView(R.layout.dialog_classement);     

            btnValid = (Button) findViewById(R.id.btnValidClassement);
            btnBack = (Button) findViewById(R.id.btnBackClassement);    
            btnRestore = (Button) findViewById(R.id.btnRestoreClassement);
            btnModify = (Button) findViewById(R.id.btnModifyClassement);

    }
}

and the activity (cut because too long):

        //create dialog
 public void getMailInformations(View v, Context context){

  currentMail = (MailHeader) v.getTag();

  dial = new MailDialog(context);

  dial.setTitle("Classement");
  dial.show();

  btnValidClassement = (Button) dial.findViewById(R.id.btnValidClassement);
  btnValidClassement.setOnClickListener(this);

 }

/** the Onclick : */

    public void onClick(View view) {

//THIS WORKS (Activity Button)
    if (view == lblPreviousMails) {

            positionList -= 20;
            // create Intent
            Intent defineIntentDisplayPreviousMails = new Intent(HeaderMailDisplayActivity.this, HeaderMailDisplayActivity.class);

            //Create bundle to pass informations to the other activity
            Bundle objetbundle = new Bundle();
            objetbundle.putString("positionList", String.valueOf(positionList));
            objetbundle.putStringArrayList("currentMails", seqnumList);
            objetbundle.putString("uidh", uidh);
            defineIntentDisplayPreviousMails.putExtras(objetbundle);

            // call headermailDisplayActivity
            HeaderMailDisplayActivity.this.startActivity(defineIntentDisplayPreviousMails);

        }

//THIS DOESN'T WORKS (Dialog Button)
    if(view == btnValidClassement){

   try{

    ClassementHandlerCall classement = new               ClassementHandlerCall();
       boolean mailClassify = classement.classifyMail(AuthentificationActivity.uidh, 
         String.valueOf(currentMail.getSeqnum()), null, null);

       dial.dismiss();

       if (mailClassify == true){

                //create Intent 
                Intent defineIntentClassifyMails = new Intent(MailClassificationActivity.this, HeaderMailDisplayActivity.class);

                // Object that allows to pass person's uidh and positionList onto HeaderMailDisplayActivity
                Bundle objetbundle = new Bundle();
                objetbundle.putString("uidh",uidh);
                objetbundle.putString("positionList", String.valueOf(positionList));
                defineIntentClassifyMails.putExtras(objetbundle);

                // call HeaderMailDisplayActivity 
                MailClassificationActivity.this.startActivity(defineIntentClassifyMails);


       }

   }catch(Exception e){
    // TODO Auto-generated catch block
    Log.e("DEBUGTAG","Error occured", e);
    e.printStackTrace();
   }



  }

}


According to stack trace, it looks like that you are calling an Intent constructor with some malformed parameters.


To start activity from intent you need to add in the manifest xml file the activity with this code

<activity android:name=".youractivityname"
          android:configChanges="keyboardHidden|orientation"
          android:label="@string/app_name">
</activity>

Put the code before the application close tag in manifest.
And the code in the button click event is:

Intent myIntent = new Intent(v.getContext(), youractivity.class);
startActivityForResult(myIntent, 1);
0

精彩评论

暂无评论...
验证码 换一张
取 消