活动不适用于点击

时间:2014-12-03 12:22:21

标签: android eclipse

我一直在关注newboston android教程。我的应用程序在模拟器上运行,出现闪屏,然后是菜单。但这些活动都不适用于点击。

Menu.java

package com.thenewboston.travis;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {
    String classes[] = { "StartingPoint", "TextPlay", "Email", "Camera",
            "example4", "example5", "example6" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this,
                android.R.layout.simple_list_item_1, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub

        super.onListItemClick(l, v, position, id);
        String cheese = classes[position];
        try {
            Class ourClass = Class
                    .forName("com.thenewboston.travis. " + cheese);
            Intent ourIntent = new Intent(Menu.this, ourClass);
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Splash.java

package com.thenewboston.travis;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity {
    MediaPlayer ourSong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        ourSong = MediaPlayer
                .create(Splash.this, R.raw.underwater_splash_small);
        ourSong.start();

        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent openStartingPoint = new Intent(
                            "com.thenewboston.travis.MENU");
                    startActivity(openStartingPoint);
                }
            }

        };
        timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ourSong.release();
        finish();
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thenewboston.travis"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

         <activity
            android:name=".Splash"
            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=".StartingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.thenewboston.travis.STARTINGPOINT" />

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

        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.thenewboston.travis.MENU" />

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

        <activity
            android:name=".TextPlay"
            android:label="@string/app_name" >

        </activity>

        <activity
            android:name=".Email"
            android:label="@string/app_name" >

        </activity>

    </application>

</manifest>

TextPlay.java

package com.thenewboston.travis;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity implements View.OnClickListener {
    Button chkCmd;
    ToggleButton passTog;
    EditText input;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        baconAndEggs();

        passTog.setOnClickListener(this);
        chkCmd.setOnClickListener(this);
    }

    private void baconAndEggs() {
        // TODO Auto-generated method stub
        chkCmd = (Button) findViewById(R.id.bResults);
        passTog = (ToggleButton) findViewById(R.id.tbPassword);
        input = (EditText) findViewById(R.id.etCommands);
        display = (TextView) findViewById(R.id.tvResults);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        case R.id.bResults:
            String check = input.getText().toString();
            display.setText(check);
            if (check.contentEquals("left")) {
                display.setGravity(Gravity.LEFT);
            } else if (check.contentEquals("center")) {
                display.setGravity(Gravity.CENTER);
            } else if (check.contentEquals("right")) {
                display.setGravity(Gravity.RIGHT);
            } else if (check.contentEquals("blue")) {
                display.setTextColor(Color.BLUE);
            } else if (check.contains("WTF")) {
                Random crazy = new Random();
                display.setText("WTF!!!");
                display.setTextSize(crazy.nextInt(75));
                display.setTextColor(Color.rgb(crazy.nextInt(265),
                        crazy.nextInt(265), crazy.nextInt(265)));
                switch (crazy.nextInt(3)) {
                case 0:
                    display.setGravity(Gravity.LEFT);
                    break;

                case 1:
                    display.setGravity(Gravity.CENTER);
                    break;

                case 2:
                    display.setGravity(Gravity.RIGHT);
                    break;
                }
            } else {
                display.setText("invalid");
                display.setGravity(Gravity.CENTER);
                display.setTextColor(Color.WHITE);
            }
            break;

        case R.id.tbPassword:
            if (passTog.isChecked()) {
                input.setInputType(InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            } else {
                input.setInputType(InputType.TYPE_CLASS_TEXT);
            }
            break;

        }
    }
}

logcat

12-03 17:46:24.512: DEBUG/AndroidRuntime(392): CheckJNI is ON
12-03 17:46:24.813: DEBUG/AndroidRuntime(392): --- registering native functions ---
12-03 17:46:25.932: DEBUG/dalvikvm(217): GC_EXPLICIT freed 130 objects / 7016 bytes in 90ms
12-03 17:46:26.243: DEBUG/PackageParser(59): Scanning package: /data/app/vmdl70360.tmp
12-03 17:46:26.662: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 16255 objects / 1127560 bytes in 113ms
12-03 17:46:27.793: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 14115 objects / 692520 bytes in 114ms
12-03 17:46:28.092: INFO/PackageManager(59): Removing non-system package:com.thenewboston.travis
12-03 17:46:28.092: INFO/ActivityManager(59): Force stopping package com.thenewboston.travis uid=10041
12-03 17:46:28.102: INFO/Process(59): Sending signal. PID: 380 SIG: 9
12-03 17:46:28.132: INFO/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in com.thenewboston.travis
12-03 17:46:28.132: INFO/WindowManager(59): WIN DEATH: Window{4a25b718 com.thenewboston.travis/com.thenewboston.travis.Menu paused=false}
12-03 17:46:28.163: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 380 uid 10041
12-03 17:46:28.312: DEBUG/PackageManager(59): Scanning package com.thenewboston.travis
12-03 17:46:28.322: INFO/PackageManager(59): Package com.thenewboston.travis codePath changed from /data/app/com.thenewboston.travis-1.apk to /data/app/com.thenewboston.travis-2.apk; Retaining data and using new
12-03 17:46:28.322: INFO/PackageManager(59): /data/app/com.thenewboston.travis-2.apk changed; unpacking
12-03 17:46:28.372: DEBUG/installd(35): DexInv: --- BEGIN '/data/app/com.thenewboston.travis-2.apk' ---
12-03 17:46:29.422: DEBUG/dalvikvm(399): DexOpt: couldn't find field Landroid/app/Notification;.category
12-03 17:46:29.422: DEBUG/dalvikvm(399): DexOpt: couldn't find field Landroid/app/Notification;.fullScreenIntent
12-03 17:46:29.422: DEBUG/dalvikvm(399): DexOpt: couldn't find field Landroid/app/Notification;.actions
12-03 17:46:29.523: DEBUG/dalvikvm(399): DexOpt: couldn't find static field
12-03 17:46:29.523: DEBUG/dalvikvm(399): DexOpt: couldn't find field Landroid/graphics/BitmapFactory$Options;.inMutable
12-03 17:46:30.423: DEBUG/dalvikvm(399): DexOpt: load 329ms, verify 1027ms, opt 55ms
12-03 17:46:30.492: DEBUG/installd(35): DexInv: --- END '/data/app/com.thenewboston.travis-2.apk' (success) ---
12-03 17:46:30.492: WARN/PackageManager(59): Code path for pkg : com.thenewboston.travis changing from /data/app/com.thenewboston.travis-1.apk to /data/app/com.thenewboston.travis-2.apk
12-03 17:46:30.502: WARN/PackageManager(59): Resource path for pkg : com.thenewboston.travis changing from /data/app/com.thenewboston.travis-1.apk to /data/app/com.thenewboston.travis-2.apk
12-03 17:46:30.502: DEBUG/PackageManager(59):   Activities: com.thenewboston.travis.Splash com.thenewboston.travis.StartingPoint com.thenewboston.travis.Menu com.thenewboston.travis.TextPlay com.thenewboston.travis.Email
12-03 17:46:30.512: INFO/ActivityManager(59): Force stopping package com.thenewboston.travis uid=10041
12-03 17:46:30.603: INFO/installd(35): move /data/dalvik-cache/data@app@com.thenewboston.travis-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.thenewboston.travis-2.apk@classes.dex
12-03 17:46:30.603: DEBUG/PackageManager(59): New package installed in /data/app/com.thenewboston.travis-2.apk
12-03 17:46:30.713: INFO/ActivityManager(59): Force stopping package com.thenewboston.travis uid=10041
12-03 17:46:30.763: DEBUG/dalvikvm(116): GC_EXPLICIT freed 80 objects / 3536 bytes in 42ms
12-03 17:46:30.913: DEBUG/dalvikvm(158): GC_EXPLICIT freed 1971 objects / 100088 bytes in 137ms
12-03 17:46:31.042: DEBUG/dalvikvm(59): GC_EXPLICIT freed 7653 objects / 464840 bytes in 145ms
12-03 17:46:31.042: INFO/installd(35): unlink /data/dalvik-cache/data@app@com.thenewboston.travis-1.apk@classes.dex
12-03 17:46:31.062: WARN/RecognitionManagerService(59): no available voice recognition services found
12-03 17:46:31.062: DEBUG/AndroidRuntime(392): Shutting down VM
12-03 17:46:31.072: DEBUG/dalvikvm(392): Debugger has detached; object registry had 1 entries
12-03 17:46:31.452: DEBUG/AndroidRuntime(405): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
12-03 17:46:31.452: DEBUG/AndroidRuntime(405): CheckJNI is ON
12-03 17:46:31.563: DEBUG/AndroidRuntime(405): --- registering native functions ---
12-03 17:46:32.053: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.thenewboston.travis/.Splash }
12-03 17:46:32.113: INFO/ActivityManager(59): Start proc com.thenewboston.travis for activity com.thenewboston.travis/.Splash: pid=411 uid=10041 gids={}
12-03 17:46:32.113: DEBUG/AndroidRuntime(405): Shutting down VM
12-03 17:46:32.123: DEBUG/dalvikvm(405): Debugger has detached; object registry had 1 entries
12-03 17:46:32.153: INFO/dalvikvm(405): JNI: AttachCurrentThread (from ???.???)
12-03 17:46:32.153: INFO/AndroidRuntime(405): NOTE: attach of thread 'Binder Thread #3' failed
12-03 17:46:32.213: DEBUG/dalvikvm(33): GC_EXPLICIT freed 262 objects / 10056 bytes in 112ms
12-03 17:46:32.323: DEBUG/dalvikvm(33): GC_EXPLICIT freed 22 objects / 968 bytes in 77ms
12-03 17:46:32.542: DEBUG/dalvikvm(33): GC_EXPLICIT freed 2 objects / 64 bytes in 193ms
12-03 17:46:32.772: DEBUG/dalvikvm(411): GC_EXTERNAL_ALLOC freed 1051 objects / 72808 bytes in 50ms
12-03 17:46:32.843: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
12-03 17:46:32.953: INFO/ActivityManager(59): Displayed activity com.thenewboston.travis/.Splash: 857 ms (total 857 ms)
12-03 17:46:34.285: ERROR/MP3Extractor(34): Unable to resync. Signalling end of stream.
12-03 17:46:37.873: INFO/ActivityManager(59): Starting activity: Intent { act=com.thenewboston.travis.MENU cmp=com.thenewboston.travis/.Menu }
12-03 17:46:38.353: INFO/ActivityManager(59): Displayed activity com.thenewboston.travis/.Menu: 438 ms (total 438 ms)
12-03 17:46:41.403: WARN/System.err(411): java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint
12-03 17:46:41.413: WARN/System.err(411):     at java.lang.Class.classForName(Native Method)
12-03 17:46:41.423: WARN/System.err(411):     at java.lang.Class.forName(Class.java:235)
12-03 17:46:41.423: WARN/System.err(411):     at java.lang.Class.forName(Class.java:182)
12-03 17:46:41.433: WARN/System.err(411):     at com.thenewboston.travis.Menu.onListItemClick(Menu.java:30)
12-03 17:46:41.433: WARN/System.err(411):     at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-03 17:46:41.453: WARN/System.err(411):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-03 17:46:41.453: WARN/System.err(411):     at android.widget.ListView.performItemClick(ListView.java:3382)
12-03 17:46:41.453: WARN/System.err(411):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-03 17:46:41.463: WARN/System.err(411):     at android.os.Handler.handleCallback(Handler.java:587)
12-03 17:46:41.463: WARN/System.err(411):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 17:46:41.463: WARN/System.err(411):     at android.os.Looper.loop(Looper.java:123)
12-03 17:46:41.463: WARN/System.err(411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 17:46:41.463: WARN/System.err(411):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:46:41.473: WARN/System.err(411):     at java.lang.reflect.Method.invoke(Method.java:521)
12-03 17:46:41.483: WARN/System.err(411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 17:46:41.483: WARN/System.err(411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 17:46:41.483: WARN/System.err(411):     at dalvik.system.NativeStart.main(Native Method)
12-03 17:46:41.483: WARN/System.err(411): Caused by: java.lang.NoClassDefFoundError: com.thenewboston.travis. StartingPoint
12-03 17:46:41.493: WARN/System.err(411):     ... 17 more
12-03 17:46:41.503: WARN/System.err(411): Caused by: java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint in loader dalvik.system.PathClassLoader[/data/app/com.thenewboston.travis-2.apk]
12-03 17:46:41.503: WARN/System.err(411):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-03 17:46:41.523: WARN/System.err(411):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-03 17:46:41.523: WARN/System.err(411):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-03 17:46:41.523: WARN/System.err(411):     ... 17 more
12-03 17:46:42.342: WARN/System.err(411): java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint
12-03 17:46:42.342: WARN/System.err(411):     at java.lang.Class.classForName(Native Method)
12-03 17:46:42.362: WARN/System.err(411):     at java.lang.Class.forName(Class.java:235)
12-03 17:46:42.362: WARN/System.err(411):     at java.lang.Class.forName(Class.java:182)
12-03 17:46:42.362: WARN/System.err(411):     at com.thenewboston.travis.Menu.onListItemClick(Menu.java:30)
12-03 17:46:42.382: WARN/System.err(411):     at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-03 17:46:42.382: WARN/System.err(411):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-03 17:46:42.382: WARN/System.err(411):     at android.widget.ListView.performItemClick(ListView.java:3382)
12-03 17:46:42.392: WARN/System.err(411):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-03 17:46:42.392: WARN/System.err(411):     at android.os.Handler.handleCallback(Handler.java:587)
12-03 17:46:42.392: WARN/System.err(411):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 17:46:42.392: WARN/System.err(411):     at android.os.Looper.loop(Looper.java:123)
12-03 17:46:42.392: WARN/System.err(411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 17:46:42.412: WARN/System.err(411):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:46:42.412: WARN/System.err(411):     at java.lang.reflect.Method.invoke(Method.java:521)
12-03 17:46:42.412: WARN/System.err(411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 17:46:42.412: WARN/System.err(411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 17:46:42.422: WARN/System.err(411):     at dalvik.system.NativeStart.main(Native Method)
12-03 17:46:42.422: WARN/System.err(411): Caused by: java.lang.NoClassDefFoundError: com.thenewboston.travis. StartingPoint
12-03 17:46:42.432: WARN/System.err(411):     ... 17 more
12-03 17:46:42.442: WARN/System.err(411): Caused by: java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint in loader dalvik.system.PathClassLoader[/data/app/com.thenewboston.travis-2.apk]
12-03 17:46:42.442: WARN/System.err(411):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-03 17:46:42.452: WARN/System.err(411):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-03 17:46:42.462: WARN/System.err(411):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-03 17:46:42.462: WARN/System.err(411):     ... 17 more
12-03 17:46:42.792: WARN/System.err(411): java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint
12-03 17:46:42.792: WARN/System.err(411):     at java.lang.Class.classForName(Native Method)
12-03 17:46:42.822: WARN/System.err(411):     at java.lang.Class.forName(Class.java:235)
12-03 17:46:42.832: WARN/System.err(411):     at java.lang.Class.forName(Class.java:182)
12-03 17:46:42.843: WARN/System.err(411):     at com.thenewboston.travis.Menu.onListItemClick(Menu.java:30)
12-03 17:46:42.843: WARN/System.err(411):     at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-03 17:46:42.853: WARN/System.err(411):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-03 17:46:42.853: WARN/System.err(411):     at android.widget.ListView.performItemClick(ListView.java:3382)
12-03 17:46:42.862: WARN/System.err(411):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-03 17:46:42.872: WARN/System.err(411):     at android.os.Handler.handleCallback(Handler.java:587)
12-03 17:46:42.872: WARN/System.err(411):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 17:46:42.872: WARN/System.err(411):     at android.os.Looper.loop(Looper.java:123)
12-03 17:46:42.872: WARN/System.err(411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 17:46:42.883: WARN/System.err(411):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 17:46:42.883: WARN/System.err(411):     at java.lang.reflect.Method.invoke(Method.java:521)
12-03 17:46:42.883: WARN/System.err(411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 17:46:42.883: WARN/System.err(411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 17:46:42.883: WARN/System.err(411):     at dalvik.system.NativeStart.main(Native Method)
12-03 17:46:42.903: WARN/System.err(411): Caused by: java.lang.NoClassDefFoundError: com.thenewboston.travis. StartingPoint

1 个答案:

答案 0 :(得分:1)

错误是:

java.lang.ClassNotFoundException: com.thenewboston.travis. StartingPoint

你的班级是

com.thenewboston.travis.StartingPoint

因此,当您获得课程时,请删除额外的空间:

Class ourClass = Class .forName("com.thenewboston.travis. " + cheese);

将其更改为:

Class ourClass = Class.forName("com.thenewboston.travis." + cheese);

希望这有帮助。