0

我正在尝试将图像添加到活动背景,这样如果没有互联网连接,它也会显示全屏图像。我有图像,但我无法显示它。这是我的代码。

    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
4

1 回答 1

0

为了将此图像添加到您的布局中,您首先需要为您的活动布局分配一个 ID。然后使用以下方法找到它:

findViewByid(id);

例如:

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

然后将 ImageView 添加到布局中:

linearLayout.addView(iv);
于 2013-04-07T21:54:53.667 回答