简单的Android ImageView淡出Button

时间:2014-06-28 19:43:55

标签: android

我是Java和Android的新手。我正在尝试构建一个具有ImageView和Button的简单应用程序。当按下按钮时,我希望图像淡出,当再次按下按钮时,我希望图像淡入。下面是我尝试使用的代码,但我在LogCat中一直遇到致命的异常错误。有人可以帮忙吗?

package com.erinkabbash.helloworld;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
            ActionBar actionBar = getActionBar();
            actionBar.hide();
        }

        Button buttonFade = (Button) findViewById(R.id.operateButton);
        final ImageView image = (ImageView) findViewById(R.id.helloWorldImage);

        final Animation animationFadeIn = AnimationUtils.loadAnimation(this,
                R.anim.fadein);
        final Animation animationFadeOut = AnimationUtils.loadAnimation(this,
                R.anim.fadeout);
        buttonFade.setOnClickListener(new Button.OnClickListener() {
            int i = 0;
            @Override
            public void onClick(View arg0) {

                if (i == 0) {
                    image.startAnimation(animationFadeOut);
                    i++;
                } else {
                    image.startAnimation(animationFadeIn);
                    i--;
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }




    }



}

logcat的:

06-28 12:50:56.379: W/ApplicationPackageManager(3021): getCSCPackageItemText()

06-28 12:50:56.429: E/MoreInfoHPW_ViewGroup(3021): Parent view is not a TextView

06-28 12:50:56.439: D/AndroidRuntime(3021): Shutting down VM

06-28 12:50:56.449: W/dalvikvm(3021): threadid=1: thread exiting with uncaught exception (group=0x41e70da0)

06-28 12:50:56.449: E/AndroidRuntime(3021): FATAL EXCEPTION: main

06-28 12:50:56.449: E/AndroidRuntime(3021): Process: com.erinkabbash.helloworld, PID: 3021

06-28 12:50:56.449: E/AndroidRuntime(3021): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erinkabbash.helloworld/com.erinkabbash.helloworld.MainActivity}: java.lang.NullPointerException

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread.access$800(ActivityThread.java:157)
06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.os.Handler.dispatchMessage(Handler.java:102)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.os.Looper.loop(Looper.java:157)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread.main(ActivityThread.java:5293)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at java.lang.reflect.Method.invokeNative(Native Method)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at java.lang.reflect.Method.invoke(Method.java:515)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at dalvik.system.NativeStart.main(Native Method)

06-28 12:50:56.449: E/AndroidRuntime(3021): Caused by: java.lang.NullPointerException

06-28 12:50:56.449: E/AndroidRuntime(3021):     at com.erinkabbash.helloworld.MainActivity.onCreate(MainActivity.java:39)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.Activity.performCreate(Activity.java:5389)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

06-28 12:50:56.449: E/AndroidRuntime(3021):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)

1 个答案:

答案 0 :(得分:0)

我将xml从fragment_main.xml复制到activity_main.xml