Android webview与弹出窗口强制关闭

时间:2014-01-15 12:54:31

标签: android xml imagebutton

我正在使用Android应用程序进行网页浏览。它包含一个图像按钮。单击图像按钮,它将在Web视图上显示一个弹出窗口。弹出窗口包含编辑文本字段。单击提交按钮,编辑文本值将作为邮件发送,弹出窗口将被取消。我遇到的问题是我无法阅读编辑文本值。当它使用该应用程序是强制关闭..如何解决这个问题,请帮助我,谢谢。

这是我的代码

        public class WebViewExample extends Activity {
   private WebView mWebView;
   private ImageView image;
   String str5;
    private PopupWindow mpopup;
    ProgressBar progressBar;
    Context context;
    /** Called when the activity is first created. */


     @Override
       public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    if(isNetworkStatusAvialable (getApplicationContext())) {
        mWebView = (WebView) findViewById(R.id.webview);
         progressBar = (ProgressBar) findViewById(R.id.progressBar1);
         image =(ImageView)findViewById(R.id.image);


            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.setWebViewClient(new wapWebViewClient());
            mWebView.loadUrl("http://facebook.com//");

            mWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int progress) 
                   {
                   if(progress < 100 && progressBar.getVisibility() == View.GONE){
                       progressBar.setVisibility(View.VISIBLE);

                   }
                   progressBar.setProgress(progress);
                   if(progress == 100) {
                       mWebView.setVisibility(View.VISIBLE);
                       progressBar.setVisibility(View.GONE);
                   }
                   }
               });


         } else {
                 Toast.makeText(getApplicationContext(), "internet is not avialable ", Toast.LENGTH_LONG).show();

            }
              ImageButton   imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
             ImageButton    imageButton2 = (ImageButton) findViewById(R.id.imageButton2);

       imageButton1.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

               Intent feedbackEmail = new Intent(Intent.ACTION_SEND);

             feedbackEmail.setType("text/email");
             feedbackEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {"prince@gmail.com"});
              feedbackEmail.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
             startActivity(Intent.createChooser(feedbackEmail, "Send Feedback:"));
        }
       });

          imageButton2.setOnClickListener(new OnClickListener() {


             public void onClick(View v) {
               View popUpView = getLayoutInflater().inflate(R.layout.popup, null); // inflating popup layout
               mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); //Creation of popup

                 mpopup.showAtLocation(mWebView, Gravity.CENTER, 0, 0);    // Displaying popup
                ImageButton Sub = (ImageButton) popUpView.findViewById(R.id.submit); 
                 EditText t1=(EditText)findViewById(R.id.editText1);
                 EditText t2=(EditText)findViewById(R.id.editText2);
                 EditText t3=(EditText)findViewById(R.id.editText3);

                String str1=t1.getText().toString();
                String str2=t2.getText().toString();
                String str3=t3.getText().toString();
                String str4=t3.getText().toString();
                 str5="name:"+str1+"  "+"email:"+str2+"  "+"number:"+str3+"  "+"idea:"+str4;
                Sub.setOnClickListener(new OnClickListener() {


                public void onClick(View v) {

                    Intent email = new Intent(Intent.ACTION_SEND);

                    email.setType("text/email");
                    email.putExtra(Intent.EXTRA_EMAIL, new String[] {"prince@gmail.com"});
                     email.putExtra(Intent.EXTRA_SUBJECT, "I have an Idea");
                     email.putExtra(Intent.EXTRA_TEXT, str5);  
                       startActivity(Intent.createChooser(email,"Send Your Idea"));

                     mpopup.dismiss();

                }
             }); 
         }
         });

     }

      private void incrementPercentage(){
       int mProgressStatus=0;
       mProgressStatus++;//i declared it as a private Integer on the activity class.
       progressBar.setProgress(mProgressStatus);
     }



       public static boolean isNetworkStatusAvialable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
             if (connectivityManager != null) 
       {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
            if(netInfos.isConnected()) 
               return true;
        }
       return false;
   }

        private class wapWebViewClient extends WebViewClient {

     @Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
     // view.loadUrl(url);
          return false;
      }

     @Override
        public void onPageFinished(WebView view, String url) {
      // when the page loaded splash screen has been invisible
     // mWebView.setVisibility(View.VISIBLE);   
      progressBar.setVisibility(View.GONE);
      image.setVisibility(View.GONE);

     }

        @Override
        public void onReceivedError(WebView view, int errorCode,
               String description, String failingUrl) {
          // if any error occured this message will be showed
             Toast.makeText(WebViewExample.this, "Error is occured, please try again..." + description, Toast.LENGTH_LONG).show();
       }
     }

      @Override
         public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
          // setting of back button
      mWebView.goBack();
             return true;
           }
           return super.onKeyDown(keyCode, event);
          }
        }

这里是y xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:orientation="vertical" >

    <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/name"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignParentTop="true"
    android:layout_marginTop="42dp"
    android:ems="10" />

   <EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:hint="@string/email" />

    <EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="19dp"
    android:ems="10"
    android:hint="@string/phone" />




   <EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="34dp"
    android:ems="10"
    android:hint="@string/idea"
    android:inputType="textMultiLine" />

   <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/submit"
    android:layout_marginRight="16dp"
    android:layout_marginTop="15dp"
    android:text="@string/hading" />

   <ImageButton
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="43dp"
    android:background="@null"
    android:src="@drawable/submit" />

  </RelativeLayout>

new xml

    <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:orientation="vertical" >

    <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:hint="@string/name"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp"
    android:ems="10" />

   <EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/email"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="30dp"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/phone"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="30dp"
    android:ems="10" />



<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:hint="@string/idea"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="31dp"
    android:ems="10"
    android:inputType="textMultiLine" />

<ImageButton
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="51dp"
    android:src="@drawable/submit" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="72dp"
    android:text="@string/hading" />

     </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

我可以从代码中获得错误:

替换

EditText t1=(EditText)findViewById(R.id.editText1);
EditText t2=(EditText)findViewById(R.id.editText2);
EditText t3=(EditText)findViewById(R.id.editText3);

with

EditText t1=(EditText)popUpView.findViewById(R.id.editText1);
EditText t2=(EditText)popUpView.findViewById(R.id.editText2);
EditText t3=(EditText)popUpView.findViewById(R.id.editText3);

您在这些edittexts中得到null。因为那些观点都在popu中,你正试图从主要活动中获取它们。

在onClick of dialog的提交中,应该在字符串中检索edittexts的值:

Sub.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        String str1=t1.getText().toString();
        String str2=t2.getText().toString();
        String str3=t3.getText().toString();
        String str4=t3.getText().toString();
        str5="name:"+str1+"  "+"email:"+str2+"  "+"number:"+str3+"  "+"idea:"+str4;

        //Email intent goes here...
    }
});

希望这有帮助。