簡體   English   中英

Android - 應用程序在添加按鈕后不斷崩潰

[英]Android - App keeps crashing after adding button

我是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

希望有人能夠找出問題所在:/謝謝。

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

您正在嘗試將ImageButton轉換為Button 看看日志

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

查看ImageButton引用,您可以看到ImageView不會擴展Button

改變這個:

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

為了這:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM