2014-01-15 46 views
0

我正在爲web view.It使用一個android應用程序,它包含一個圖像按鈕。在點擊圖像按鈕時,它將在網絡視圖上顯示一個彈出窗口。彈出窗口包含編輯文本字段。單擊提交按鈕時,編輯文本值將作爲郵件發送,彈出窗口將被取消。我面臨的問題是我不能閱讀編輯文本values.When它使用的應用程序是強制關閉..如何解決這個問題,請幫助我,謝謝。Android webview與彈出窗口強制關閉

這裏是我的代碼

 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[] {"[email protected]"}); 
       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[] {"[email protected]"}); 
        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> 

新的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> 
+0

「當它使用的應用程序是強制關閉」告訴我,這個問題應該有某種堆棧跟蹤。 – keyboardsurfer

+0

@doubter ::請發佈您的logcat和代碼你做了什麼 – KMI

+0

看到我的新編輯 – doubter

回答

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); 

您在這些edittext中得到空值。因爲這些觀點是在流行,你正試圖讓他們從主要活動。

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... 
    } 
}); 

希望這有助於:

而且edittexts的值應該在字符串中的對話的提交的檢索中的onClick。

+0

謝謝你sir.now問題解決了......我還有一個dbt ...彈出如何做全屏? – doubter

+0

@doubter你可以在xml的彈出框中設置寬度和高度爲match_parent。 –

+0

它已經匹配父母 – doubter