Android - 应用程序在添加按钮后不断崩溃

时间:2015-09-03 15:19:35

标签: java android

我是Android新手。这实际上是我的第一个应用。 所以我目前正在制作一个非常基本的Webview应用程序。

我的activity_main.xml非常简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="0dp"
        android:layout_alignParentTop="true" />

    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignTop="@+id/webView"
        android:layout_alignLeft="@+id/webView"
        android:layout_alignStart="@+id/webView"
        android:layout_alignRight="@+id/webView"
        android:layout_alignEnd="@+id/webView" />

</RelativeLayout>

使用MainActivity.java文件。

public class MainActivity extends ActionBarActivity {

    private WebView mWebView;

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

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

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.loadUrl("http://example.com");

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new tnwWebViewClient());

        mWebView.setWebViewClient(new tnwWebViewClient() {

            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
            }

            public void onPageFinished(WebView view, String url) {
                findViewById(R.id.progressBar).setVisibility(View.GONE);

                findViewById(R.id.webView).setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}

该应用程序与上面的代码完美匹配。 将以下代码添加到上面的

后会出现问题

activity_main.xml

<ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/homeButton"
        android:background="@drawable/home"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="20dp" />

(上面的代码运行没有错误,但在添加以下内容时崩溃)

 Button newButton = (Button)findViewById(R.id.homeButton);
        newButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mWebView.loadUrl("http://example.com");
            }
        });

在上述之后,应用程序只是崩溃了。我无法弄清楚logcat的含义,但现在是:

09-03 23:13:10.348      752-846/? I/PackageManager﹕ Package com.uziiuzair.tnw codePath changed from /data/app/com.uziiuzair.tnw-2 to /data/app/com.uziiuzair.tnw-1; Retaining data and using new
09-03 23:13:10.403      752-846/? I/PackageManager﹕ Running dexopt on: /data/app/com.uziiuzair.tnw-1/base.apk pkg=com.uziiuzair.tnw isa=arm vmSafeMode=false
09-03 23:13:10.436    1320-1320/? I/dex2oat﹕ /system/bin/dex2oat --zip-fd=6 --zip-location=/data/app/com.uziiuzair.tnw-1/base.apk --oat-fd=7 --oat-location=/data/dalvik-cache/arm/data@app@com.uziiuzair.tnw-1@base.apk@classes.dex --instruction-set=arm --instruction-set-features=div --runtime-arg -Xms64m --runtime-arg -Xmx512m --swap-fd=8
09-03 23:13:11.930      752-772/? I/ActivityManager﹕ Force stopping com.uziiuzair.tnw appid=10267 user=-1: update pkg
09-03 23:13:11.931      752-846/? W/PackageManager﹕ Code path for pkg : com.uziiuzair.tnw changing from /data/app/com.uziiuzair.tnw-2 to /data/app/com.uziiuzair.tnw-1
09-03 23:13:11.931      752-846/? W/PackageManager﹕ Resource path for pkg : com.uziiuzair.tnw changing from /data/app/com.uziiuzair.tnw-2 to /data/app/com.uziiuzair.tnw-1
09-03 23:13:12.104      752-846/? I/ActivityManager﹕ Force stopping com.uziiuzair.tnw appid=10267 user=0: pkg removed
09-03 23:13:12.264      752-846/? W/ActivityManager﹕ Skipping broadcast of Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.uziiuzair.tnw flg=0x4000010 (has extras) }: user 10 is stopped
09-03 23:13:12.280      752-752/? D/BackupManagerService﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.uziiuzair.tnw flg=0x4000010 (has extras) }
09-03 23:13:12.281      752-752/? D/BackupManagerService﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.uziiuzair.tnw flg=0x4000010 (has extras) }
09-03 23:13:12.282      752-846/? W/ActivityManager﹕ Skipping broadcast of Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.uziiuzair.tnw flg=0x4000010 (has extras) }: user 10 is stopped
09-03 23:13:12.298      752-752/? W/BackupManagerService﹕ Removing schedule queue dupe of com.uziiuzair.tnw
09-03 23:13:12.300      752-846/? W/ActivityManager﹕ Skipping broadcast of Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:com.uziiuzair.tnw flg=0x4000010 (has extras) }: user 10 is stopped
09-03 23:13:12.307      752-846/? W/ActivityManager﹕ Skipping broadcast of Intent { act=android.intent.action.MY_PACKAGE_REPLACED flg=0x4000010 pkg=com.uziiuzair.tnw (has extras) }: user 10 is stopped
09-03 23:13:13.052     752-1439/? I/ActivityManager﹕ Force stopping com.uziiuzair.tnw appid=10267 user=-1: set debug app
09-03 23:13:13.053     752-1439/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.uziiuzair.tnw/.MainActivity} from uid 2000 on display 0
09-03 23:13:13.073     752-1439/? V/WindowManager﹕ addAppToken: AppWindowToken{9f61ede token=Token{b559e19 ActivityRecord{b70f960 u0 com.uziiuzair.tnw/.MainActivity t9258}}} to stack=1 task=9258 at 0
09-03 23:13:13.099      752-768/? I/ActivityManager﹕ Start proc 1390:com.uziiuzair.tnw/u0a267 for activity com.uziiuzair.tnw/.MainActivity
09-03 23:13:13.108      752-779/? V/WindowManager﹕ Adding window Window{a73f751 u0 Starting com.uziiuzair.tnw} at 27 of 31 (after Window{306fc06c u0 com.android.settings/com.android.settings.SubSettings})
09-03 23:13:13.182    1390-1390/? W/ActivityThread﹕ Application com.uziiuzair.tnw is waiting for the debugger on port 8100...
09-03 23:13:13.618    2037-1456/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.uziiuzair.tnw
09-03 23:13:13.635    2037-2037/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.uziiuzair.tnw cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
09-03 23:13:15.385    1390-1390/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.uziiuzair.tnw, PID: 1390
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uziiuzair.tnw/com.uziiuzair.tnw.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
            at com.uziiuzair.tnw.MainActivity.onCreate(MainActivity.java:32)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
            at android.app.ActivityThread.access$900(ActivityThread.java:154)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
09-03 23:13:15.386      752-768/? W/ActivityManager﹕ Force finishing activity 1 com.uziiuzair.tnw/.MainActivity
09-03 23:13:15.695      752-768/? I/WindowManager﹕ Screenshot max retries 4 of Token{b559e19 ActivityRecord{b70f960 u0 com.uziiuzair.tnw/.MainActivity t9258 f}} appWin=Window{a73f751 u0 Starting com.uziiuzair.tnw} drawState=4
09-03 23:13:16.198      752-772/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{b70f960 u0 com.uziiuzair.tnw/.MainActivity t9258 f}
09-03 23:13:16.735    2037-1844/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.uziiuzair.tnw
09-03 23:13:16.860    2037-2037/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.uziiuzair.tnw cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
09-03 23:13:16.890    2037-2037/? D/Vision﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.uziiuzair.tnw flg=0x4000010 cmp=com.google.android.gms/.vision.DependencyBroadcastReceiverProxy (has extras) }
09-03 23:13:16.890    2037-2037/? D/Vision﹕ Failed to find package metadata for com.uziiuzair.tnw
09-03 23:13:17.065   13910-1891/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=com.uziiuzair.tnw, CONTACTS=MAYBE
09-03 23:13:17.275    1145-1145/? W/PackageManager﹕ Failure retrieving resources for com.uziiuzair.tnw: Resource ID #0x0
09-03 23:13:18.572    2037-2058/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.uziiuzair.tnw
09-03 23:13:18.576    2037-2037/? D/Vision﹕ Received broadcast Intent { act=android.intent.action.PACKAGE_REPLACED dat=package:com.uziiuzair.tnw flg=0x4000010 cmp=com.google.android.gms/.vision.DependencyBroadcastReceiverProxy (has extras) }
09-03 23:13:18.581    2037-2037/? D/Vision﹕ Failed to find package metadata for com.uziiuzair.tnw
09-03 23:13:21.586     752-1405/? I/ActivityManager﹕ Killing 1390:com.uziiuzair.tnw/u0a267 (adj 15): empty #17
09-03 23:13:22.341    2400-2400/? D/OnUpgrade﹕ onReceive --- > package name (data string): package:com.uziiuzair.tnw

希望有人能够弄清楚问题所在:/ 感谢。

2 个答案:

答案 0 :(得分:2)

Logcat中的关键行是

java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

您正尝试将ImageButton投射到此行中的Button

Button newButton = (Button)findViewById(R.id.homeButton);

将其更改为以下

ImageButton newButton = (ImageButton)findViewById(R.id.homeButton);

答案 1 :(得分:1)

您正在尝试将ImageButton投射到Button。 看一下日志

ImageButton cannot be cast to android.widget.Button
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)

查看ImageButton reference,您可以看到ImageView未延伸Button

改变这个:

Button newButton = (Button)findViewById(R.id.homeButton);

为此:

ImageButton newButton = (ImageButton)findViewById(R.id.homeButton);