2013-04-07 50 views
0

我試圖將圖像添加到活動背景,以便如果沒有互聯網連接,它也會顯示全屏圖像。我有這個形象,但我無法讓它出現。這是我的代碼。Android - 將imageView添加到背景

public class MainActivity extends Activity { 

    private WebView webView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     webView = (WebView) findViewById(R.id.webview); 

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     android.net.NetworkInfo wifi = cm 
       .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     android.net.NetworkInfo datac = cm 
       .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
     if ((wifi != null & datac != null) 
       && (wifi.isConnected() | datac.isConnected())) { 
       CookieManager cookieManager = CookieManager.getInstance(); 
       cookieManager.setAcceptCookie(true); 

       webView = (WebView) findViewById(R.id.webview); 
       webView.getSettings().setJavaScriptEnabled(true); 
       webView.setWebChromeClient(new WebChromeClient() { 
         public void onProgressChanged(WebView view, int progress) { 
         MainActivity.this.setProgress(progress * 1000); 
         } 
        }); 
        webView.setWebViewClient(new MyWebViewClient() { 
         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
         Toast.makeText(MainActivity.this, "An error occurred " + description, Toast.LENGTH_LONG).show(); 
         } 
        }); 

        webView.loadUrl("http://mysite.com/?ma=1"); 
       } 
       else{ 
this ----->  ImageView iv = new ImageView(this); 
        iv.setImageResource(R.drawable.noconnect); 
        iv.setAdjustViewBounds(true); 
        Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG); 
        toast.show(); 

     } 

    } 

    private class MyWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      if (Uri.parse(url).getHost().equals("mysite.com")) { 
       return false; 
      } 
      // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
      startActivity(intent); 
      return true; 
     } 
    } 


    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 
      webView.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 


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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
      case R.id.logout: 
       finish(); 
       return true; 
     } 
     return false; 
    } 


} 

編輯:這裏是XML,這種方式導致它在加載時崩潰。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/pmlayout" > 

    <ImageView 
     android:id="@+id/noconn" 
     android:background="@drawable/noconnect" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="matrix" 
     android:visibility="visible" > 
    </ImageView> 

    <WebView android:id="@+id/webview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 

</LinearLayout> 

編輯2:

這是崩潰的錯誤,現在即時得到。

04-07 18:51:17.920: E/AndroidRuntime(8113): FATAL EXCEPTION: main 
04-07 18:51:17.920: E/AndroidRuntime(8113): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.site.testapp/com.site.testapp.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.webkit.WebView 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread.access$700(ActivityThread.java:139) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.os.Looper.loop(Looper.java:137) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread.main(ActivityThread.java:4918) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at dalvik.system.NativeStart.main(Native Method) 
04-07 18:51:17.920: E/AndroidRuntime(8113): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.webkit.WebView 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at me.publicize.testapp.MainActivity.onCreate(MainActivity.java:30) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.Activity.performCreate(Activity.java:5048) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052) 
04-07 18:51:17.920: E/AndroidRuntime(8113):  ... 11 more 

回答

0

爲了將此圖片添加到您的佈局,您首先需要爲您的活動佈局分配一個ID。再次

linearLayout.addView(iv); 
+0

感謝您的幫助: 然後使用發現:

findViewByid(id); 

例如:

LinearLayout linearLayout = (LinearLayout) findViewByid (R.id.llayout); 

,然後ImageView的添加到佈局。我嘗試過,但仍然無法正常工作。 ImageView iv =新的ImageView(this); iv.setImageResource(R.drawable.noconnect); LinearLayout playout =(LinearLayout)findViewById(R.id.pmlayout); playout.addView(iv); 012.iv.setAdjustViewBounds(true); – 2013-04-07 22:02:23

+0

正如我在私密文章中告訴你的,嘗試將圖像視圖放在xml文件中,並將其可見性設置爲false,然後在代碼中將其更改爲可見性而不處理此問題。它應該工作...你有任何錯誤按摩? – 2013-04-07 22:15:01

+0

好吧我添加了android:visibility =「不可見」到XML imaageView。在這個活動中,我使用這個 ImageView iv =(ImageView)findViewById(R.id.noconn); 012.V4IB.Visibility(ImageView.VISIBLE);那也行不通。爲什麼事情如此複雜。 :/ – 2013-04-07 22:27:37