我尝试加载Android首选项活动时,应用程序崩溃

时间:2015-01-11 18:32:12

标签: android xml android-preferences

当我按下设置按钮时,应用程序崩溃(在另一个布局中设置:-)。我在这里只分享相关代码。我想在这里做的是我想加载具有首选项设置的prefs活动。 任何帮助将受到高度赞赏。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dummyproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".SignIn"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.dummyproject.start_surfing" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Prefs"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.dummyproject.prefsettings" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".URIsettings"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.dummyproject.oldsettings" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

的prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
    android:title = "eText_ip"
    android:key = "ipAddr"
    android:summary="Enter the IP address of the webserver" 
    android:defaultValue="192.168.1.4"
    />

<EditTextPreference 
    android:title = "eText_port"
    android:key = "portNum"
    android:summary="Enter the port number on which the webserver is running"
    android:defaultValue = "8080"
    />

<EditTextPreference
    android:title="eText_location"
    android:key="location"
    android:summary = "Enter the name of .html/.php file that you want to run" 
    android:defaultValue="shaheer/traveller.php"
    />      

Prefs.java

package com.example.dummyproject;
import android.os.Bundle;
import android.preference.PreferenceFragment;

public class Prefs extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource (R.xml.prefs);
    }

}

SignIn.java

package com.example.dummyproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SignIn extends Activity {
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.app_login);
        // com.example.dummyproject.settings

        Button signInButton = (Button) findViewById (R.id.sign_up_btn);
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intOpenWebView = new Intent ("com.example.dummyproject.start_surfing");
                startActivity (intOpenWebView);

            }
        });

        Button settingsButton = (Button) findViewById (R.id.settings_btn);
        settingsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // THE APPLICATION CRASHES RIGHT HERE!
                Intent intOpenWebView = new Intent ("com.example.dummyproject.prefsettings");
                startActivity (intOpenWebView);
            }
        });
    }

    private int authenticate (String userName, String password) {
        return 1;
    }
}

logcat的:

01-11 22:58:30.876: I/Process(4949): Sending signal. PID: 4949 SIG: 9
01-11 23:00:57.012: D/PackageItemInfo(6004): loadIcon(cached)name=com.example.dummyproject.SignIn
01-11 23:00:57.052: D/dalvikvm(6004): GC_FOR_ALLOC freed 38K, 65% free 4474K/12760K, paused 16ms, total 17ms
01-11 23:00:57.112: I/dalvikvm-heap(6004): Grow heap (frag case) to 18.562MB for 12582928-byte allocation
01-11 23:00:57.173: D/dalvikvm(6004): GC_CONCURRENT freed 3K, 34% free 16759K/25052K, paused 22ms+11ms, total 57ms
01-11 23:00:57.733: D/libEGL(6004): loaded /system/lib/egl/libEGL_adreno200.so
01-11 23:00:57.753: D/libEGL(6004): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-11 23:00:57.763: D/libEGL(6004): loaded /system/lib/egl/libGLESv2_adreno200.so
01-11 23:00:57.773: I/Adreno200-EGL(6004): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400_msm8960_JB_2.5_CL3744273_release_AU (CL3744273)
01-11 23:00:57.773: I/Adreno200-EGL(6004): Build Date: 06/30/13 Sun
01-11 23:00:57.773: I/Adreno200-EGL(6004): Local Branch: 
01-11 23:00:57.773: I/Adreno200-EGL(6004): Remote Branch: quic/jb_2.5
01-11 23:00:57.773: I/Adreno200-EGL(6004): Local Patches: NONE
01-11 23:00:57.773: I/Adreno200-EGL(6004): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400 +  NOTHING
01-11 23:00:58.073: D/OpenGLRenderer(6004): Enabling debug mode 0
01-11 23:01:59.489: D/DEBUG(6004): Splash intenthttp://192.168.1.3:80/SemesterProject/location_page.html
01-11 23:01:59.519: D/webcoreglue(6004): netstack: Memory Cache feature is OFF
01-11 23:01:59.639: D/JSENGINE(6004): qualcomm.jsengine.version:C.2-patch35-git:7b7ad6f
01-11 23:01:59.779: D/HostStatisticManager(6004): netstack: DNS Host Prioritization is: ON, Version: 5.0.1
01-11 23:01:59.779: I/(6004): netstack: LIB_MGR - Lib loaded: libdnshostprio.so
01-11 23:01:59.779: I/(6004): netstack: STAT_HUB - Succeeded to load plugin: libdnshostprio.so
01-11 23:01:59.779: E/(6004): netstack: LIB_MGR - Error loading lib spl_proc_plugin.so
01-11 23:01:59.779: E/(6004): netstack: STAT_HUB - Failed to load plugin: spl_proc_plugin.so
01-11 23:01:59.789: I/(6004): netstack: LIB_MGR - Lib loaded: pp_proc_plugin.so
01-11 23:01:59.789: I/(6004): netstack: STAT_HUB - Succeeded to load plugin: pp_proc_plugin.so
01-11 23:01:59.789: E/(6004): netstack:  STAT_HUB - App com.example.dummyproject isn't supported
01-11 23:01:59.829: V/chromium(6004): external/chromium/net/host_resolver_helper/host_resolver_helper.cc:66: [0111/230159:INFO:host_resolver_helper.cc(66)] DNSPreResolver::Init got hostprovider:0x5d18a00c
01-11 23:01:59.829: V/chromium(6004): external/chromium/net/base/host_resolver_impl.cc:1510: [0111/230159:INFO:host_resolver_impl.cc(1510)] HostResolverImpl::SetPreresolver preresolver:0x5d300260
01-11 23:01:59.829: E/(6004): netstack: LIB_MGR - Error loading lib libsocketpoolextend.so
01-11 23:01:59.829: I/SockPoolExtend(6004): Using default ClientSocketPoolExtend
01-11 23:01:59.839: D/(6004): external/chromium/net/socket/tcp_fin_aggregation_factory.cc: libtcpfinaggr.so successfully loaded
01-11 23:01:59.839: D/(6004): external/chromium/net/socket/tcp_fin_aggregation_factory.cc,: TCP Fin Aggregation initializing method was found in libtcpfinaggr.so
01-11 23:01:59.839: D/TCPFinAggregation(6004): netstack: TCPFinAggregation is 1, Version 5.0.1
01-11 23:01:59.839: D/TCPFinAggregation(6004): system property net.tcp.fin.aggregation.wait was set, value: 20
01-11 23:01:59.839: I/CneObs(6004): CAS is enabled
01-11 23:01:59.839: I/CneObs(6004): [CNE CLIENT STATE MACHINE] transition NOT_CONNECTED_NOT_ATTEMPTED -> CONNECTING
01-11 23:01:59.839: D/CneObs(6004): failed to connect to server
01-11 23:01:59.839: I/CneObs(6004): [CNE CLIENT STATE MACHINE] transition CONNECTING -> NOT_CONNECTED_UNABLE_TO_CONNECT
01-11 23:01:59.839: D/CneObs(6004): monitorLoop: connect attempt 1
01-11 23:01:59.839: D/TCPFinAggregation(6004): system property net.tcp.fin.aggregation.close was set, value: 300
01-11 23:01:59.839: D/TCPFinAggregation(6004): netstack: CloseUnusedSockets is ON, (TCPFinAggregation), Version 5.0.1
01-11 23:01:59.939: I/CneObs(6004): [CNE CLIENT STATE MACHINE] transition NOT_CONNECTED_UNABLE_TO_CONNECT -> CONNECTING
01-11 23:01:59.939: D/CneObs(6004): failed to connect to server
01-11 23:01:59.939: I/CneObs(6004): [CNE CLIENT STATE MACHINE] transition CONNECTING -> NOT_CONNECTED_UNABLE_TO_CONNECT
01-11 23:01:59.939: D/CneObs(6004): monitorLoop: connect attempt 0
01-11 23:02:00.040: D/CneObs(6004): Monitor loop is terminating
01-11 23:02:00.090: E/CneObs(6004): communication to CAS is down
01-11 23:02:00.090: D/TCPFinAggregation(6004): Failed to get network status! received ret: -2
01-11 23:02:00.090: D/Socket_Pool(6004): netstack: CloseUnusedSockets is ON
01-11 23:02:00.090: D/Socket_Pool(6004): netstack: system net.statistics value: 0
01-11 23:02:00.090: D/Socket_Pool(6004): netstack: CloseUnusedSockets is ON
01-11 23:02:00.090: D/Socket_Pool(6004): netstack: system net.statistics value: 0
01-11 23:02:00.100: D/(6004): external/chromium/net/http/http_getzip_factory.cc: libgetzip.so successfully loaded
01-11 23:02:00.100: D/(6004): external/chromium/net/http/http_getzip_factory.cc,: GETzip initializing method was found in libgetzip.so
01-11 23:02:00.100: D/netstack(6004): netstack: Request Priority is ON
01-11 23:02:00.100: D/(6004): netstack: Getzip is: ON, Version: 5.0.1
01-11 23:02:00.160: D/(6004): netstack: Early Connections is: ON, Version: 5.0.1, Param: 3
01-11 23:02:00.280: D/DEBUG(6004): Web View has been loaded
01-11 23:02:00.350: D/DEBUG(6004): Web View has been loaded
01-11 23:02:00.370: D/TilesManager(6004): Starting TG #0, 0x5d448060
01-11 23:02:00.370: D/TilesManager(6004): new EGLContext from framework: 5cc16488 
01-11 23:02:00.370: D/GLWebViewState(6004): Reinit shader
01-11 23:02:00.470: D/GLWebViewState(6004): Reinit transferQueue
01-11 23:03:39.836: W/IInputConnectionWrapper(6004): showStatusIcon on inactive InputConnection
01-11 23:03:44.011: D/PackageItemInfo(6900): loadIcon(cached)name=com.example.dummyproject.SignIn
01-11 23:03:44.071: D/dalvikvm(6900): GC_FOR_ALLOC freed 55K, 65% free 4474K/12760K, paused 19ms, total 19ms
01-11 23:03:44.101: I/dalvikvm-heap(6900): Grow heap (frag case) to 18.562MB for 12582928-byte allocation
01-11 23:03:44.151: D/dalvikvm(6900): GC_CONCURRENT freed 3K, 34% free 16759K/25052K, paused 4ms+7ms, total 51ms
01-11 23:03:44.841: D/libEGL(6900): loaded /system/lib/egl/libEGL_adreno200.so
01-11 23:03:44.851: D/libEGL(6900): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-11 23:03:44.871: D/libEGL(6900): loaded /system/lib/egl/libGLESv2_adreno200.so
01-11 23:03:47.054: I/Adreno200-EGL(6900): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400_msm8960_JB_2.5_CL3744273_release_AU (CL3744273)
01-11 23:03:47.054: I/Adreno200-EGL(6900): Build Date: 06/30/13 Sun
01-11 23:03:47.054: I/Adreno200-EGL(6900): Local Branch: 
01-11 23:03:47.054: I/Adreno200-EGL(6900): Remote Branch: quic/jb_2.5
01-11 23:03:47.054: I/Adreno200-EGL(6900): Local Patches: NONE
01-11 23:03:47.054: I/Adreno200-EGL(6900): Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.04.02.02.040.400 +  NOTHING
01-11 23:03:47.144: D/OpenGLRenderer(6900): Enabling debug mode 0
01-11 23:04:27.157: D/AndroidRuntime(6900): Shutting down VM
01-11 23:04:27.157: W/dalvikvm(6900): threadid=1: thread exiting with uncaught exception (group=0x41635ae0)
01-11 23:04:27.157: E/AndroidRuntime(6900): FATAL EXCEPTION: main
01-11 23:04:27.157: E/AndroidRuntime(6900): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dummyproject/com.example.dummyproject.Prefs}: java.lang.ClassCastException: com.example.dummyproject.Prefs cannot be cast to android.app.Activity
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread.access$600(ActivityThread.java:145)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.os.Looper.loop(Looper.java:137)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread.main(ActivityThread.java:5099)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at java.lang.reflect.Method.invoke(Method.java:511)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at dalvik.system.NativeStart.main(Native Method)
01-11 23:04:27.157: E/AndroidRuntime(6900): Caused by: java.lang.ClassCastException: com.example.dummyproject.Prefs cannot be cast to android.app.Activity
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.Instrumentation.newActivity(Instrumentation.java:1055)
01-11 23:04:27.157: E/AndroidRuntime(6900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
01-11 23:04:27.157: E/AndroidRuntime(6900):     ... 11 more
01-11 23:04:29.369: I/Process(6900): Sending signal. PID: 6900 SIG: 9

1 个答案:

答案 0 :(得分:2)

改变这个:

Intent intOpenWebView = new Intent ("com.example.dummyproject.prefsettings");
startActivity(intOpenWebView);

到此:

Intent intent = new Intent(this, Prefs.class);
startActivity(intent);

同时从清单中删除此代码:

<intent-filter>
    <action android:name="com.example.dummyproject.start_surfing" />

    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

有关开始新活动的更多信息here

另外,您说,您发布了相关代码。没有!你发布了这么多代码,没人想读。 阅读有关此here的更多信息。当代码永远不会启动时,您不必从Prefs.java发布prefs.xml代码。

修改:

我建议作为解决方案的构造函数存在。 Here是官方文档。