android - 当我单击按钮时,无论我如何实现 onClick,我的应用程序都会崩溃

标签 android android-intent onclick android-fullscreen

    package com.tchotchke.weatherpaper;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.tchotchke.weatherpaper.util.SystemUiHider;


public class FullscreenActivity extends Activity{

    OnClickListener buttons;

    private static final boolean AUTO_HIDE = true;
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
    private static final boolean TOGGLE_ON_CLICK = true;
    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
    private SystemUiHider mSystemUiHider;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        final View controlsView = findViewById(R.id.fullscreen_content_controls);
        final View contentView = findViewById(R.id.fullscreen_content);

        Button objects_button = (Button)findViewById(R.id.objects_button);

        buttons = new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent("com.tchotchke.MYOBJECTS");
                startActivity(intent);
            }
        };
        objects_button.setOnClickListener(buttons);

        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
        mSystemUiHider.setup();
        mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener`() {`
                    // Cached values.
                    int mControlsHeight;
                    int mShortAnimTime;

                    @Override
                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                    public void onVisibilityChange(boolean visible) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                            // If the ViewPropertyAnimator API is available
                            // (Honeycomb MR2 and later), use it to animate the
                            // in-layout UI controls at the bottom of the
                            // screen.
                            if (mControlsHeight == 0) {
                                mControlsHeight = controlsView.getHeight();
                            }
                            if (mShortAnimTime == 0) {
                                mShortAnimTime = getResources().getInteger(
                                        android.R.integer.config_shortAnimTime);
                            }
                            controlsView.animate()
                                    .translationY(visible ? 0 : mControlsHeight)
                                    .setDuration(mShortAnimTime);
                        } else {
                            // If the ViewPropertyAnimator APIs aren't
                            // available, simply show or hide the in-layout UI
                            // controls.
                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                        }

                        if (visible && AUTO_HIDE) {
                            // Schedule a hide().
                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
                        }
                    }
                });

        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TOGGLE_ON_CLICK) {
                    mSystemUiHider.toggle();
                } else {
                    mSystemUiHider.show();
                } }});   }



    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        delayedHide(100);
    }

    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    Handler mHideHandler = new Handler();
    Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            mSystemUiHider.hide();
        }
    };

    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }



    }

我的 logcat 看起来像这样(单击按钮之前没有错误)

- 03-21 15:57:30.871: D/gralloc_goldfish(1034): Emulator without GPU emulation detected.

-03-21 15:57:31.812: I/Choreographer(1034): Skipped 107 frames!  The application may be doing too much work on its main thread.

-03-21 15:57:32.531: D/dalvikvm(1034): GC_CONCURRENT freed 185K, 11% free 2580K/2896K, paused 3ms+3ms, total 197ms

-03-21 15:57:51.971: I/Choreographer(1034): Skipped 32 frames!  The application may be doing too much work on its main thread.

-03-21 15:57:58.684: D/AndroidRuntime(1034): Shutting down VM

-03-21 15:57:58.692: W/dalvikvm(1034): threadid=1: thread exiting with uncaught exception (group=0x40a71930)

-03-21 15:57:58.702: E/AndroidRuntime(1034): FATAL EXCEPTION: main

-03-21 15:57:58.702: E/AndroidRuntime(1034): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.tchotchke.MYOBJECTS }

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Activity.startActivityForResult(Activity.java:3370)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Activity.startActivityForResult(Activity.java:3331)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Activity.startActivity(Activity.java:3566)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.Activity.startActivity(Activity.java:3534)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at com.tchotchke.weatherpaper.FullscreenActivity$3.onClick(FullscreenActivity.java:40)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.view.View.performClick(View.java:4204)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.view.View$PerformClick.run(View.java:17355)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.os.Handler.handleCallback(Handler.java:725)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.os.Handler.dispatchMessage(Handler.java:92)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.os.Looper.loop(Looper.java:137)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at android.app.ActivityThread.main(ActivityThread.java:5041)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at java.lang.reflect.Method.invokeNative(Native Method)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at java.lang.reflect.Method.invoke(Method.java:511)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

-03-21 15:57:58.702: E/AndroidRuntime(1034):    at dalvik.system.NativeStart.main(Native Method)

这是我的 list ,抱歉我遗漏了:

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

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

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

        <activity
            android:name="com.tchotchke.weatherpaper.FullscreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tchotchke.weatherpaper.MyObjects"
            android:label="@string/dummy_button">
            <action android:name="android.intent.action.MYOBJECTS"/>
        </activity>


    </application>

</manifest>

我不太清楚 list 的工作原理,但从我所看到的来看,它似乎是合法的

最佳答案

它没有找到您的 Activity “com.tchotchke.MYOBJECTS,您需要在 list 中声明此 Activity 。如果这是您项目中的 Activity ,则通常通过以下方式完成 Intent :

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

希望这有帮助。

关于android - 当我单击按钮时,无论我如何实现 onClick,我的应用程序都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15552629/

相关文章:

android - 如何处理 onItemClick 中的单独 View 点击?

java - 以编程方式设置新的 View 边距

android - "Back"按钮关闭与 WebView 的共享对话框和子 Activity

android - 从 Laravel 后端触发对 Android Studio 的自动响应

android - 无法将 var 传递给其他 Activity

android - ACTION_WEB_SEARCH 仅在第一次打开正确的搜索查询

javascript - 将变量从复选框传递给 onclick 函数

javascript - 为什么Javascript不添加onclick?

android - SQLite - 从 csv 文件中将带有换行符的字符串插入数据库

Android 广播接收器 : perform network call and play sound