package com.example.Lovisis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.Button;

import com.yashpoolla.Lovisis.R;

public class MainActivity extends Activity {


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

    Button a = (Button) findViewById(R.id.button3);
   a.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            //TODO Auto-generated method stub
            gotoScreen1();//startActivity(new Intent(MainActivity.this, AlarmInput.class));

        }
    });

    Button b = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            gotoScreen2();//startActivity(new Intent(MainActivity.this, Cbdata.class));
         }
    });

     Button c = (Button) findViewById(R.id.button1);
        c.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //gotoScreen4();//startActivity(new Intent(MainActivity.this, Help.class));
             }
        });

        Button d = (Button) findViewById(R.id.Button4);
        d.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //gotoScreen3();//startActivity(new Intent(MainActivity.this, Viewdb.class));
            }
        });
    }





    //anim start



    /*public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_SCREEN2, 1, R.string.menu_screen2 );
        return result;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch( id ) {
        case MENU_SCREEN2:
            gotoScreen2();
            break;
        }
        return true;
    }*/

    private void gotoScreen1() {
        View g = (View)findViewById( R.id.main);
        applyRotation( g,0,90, new GotoNextActivity1());
    }

    private void gotoScreen2() {
        View g = (View)findViewById( R.id.main);
        applyRotation( g,0,90, new GotoNextActivity2());
    }

    private void gotoScreen3() {
        View g = (View)findViewById( R.id.main);
        applyRotation( g,0,90, new GotoNextActivity3());
    }

    private void gotoScreen4() {
        View g = (View)findViewById( R.id.main);
        applyRotation( g,0,90, new GotoNextActivity4());
    }

    public static void applyRotation(
            View container,
            float start,
            float end,
            Animation.AnimationListener animationListener ) {
        // Find the center of the container
        final float centerX = container.getWidth()/2.0f;
        final float centerY = container.getHeight()/2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Rotate3dAnimation rotation =
                new Rotate3dAnimation(start, end, centerX, centerY, 10.0f, true);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        if( animationListener != null )
            rotation.setAnimationListener( animationListener );

        container.startAnimation(rotation);
    }

    private final class GotoNextActivity1 implements Animation.AnimationListener {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            startActivity(new Intent(MainActivity.this, AlarmInput.class));
            overridePendingTransition( 0,0 );
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }


    private final class GotoNextActivity2 implements Animation.AnimationListener {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            startActivity(new Intent(MainActivity.this, Cbdata.class));
            overridePendingTransition( 0,0 );
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    private final class GotoNextActivity3 implements Animation.AnimationListener {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            startActivity(new Intent(MainActivity.this, Viewdb.class));
            overridePendingTransition( 0,0 );
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    private final class GotoNextActivity4 implements Animation.AnimationListener {

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            startActivity(new Intent(MainActivity.this, Help.class));
            overridePendingTransition( 0,0 );
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }
//anim end

    }


表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yashpoolla.Lovisis"
    android:versionCode="3"
    android:versionName="1.3" >

    <uses-sdk
        android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    -

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="false">
        <receiver android:name="com.example.Lovisis.CbReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.CB_RECEIVED" >
                </action>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>

        <activity android:name="com.example.Lovisis.Cbdata" >
            <intent-filter>
                <action
                    android:name="android.appwidget.action.APPWIDGET_UPDATE"
                    android:launchMode="singleTop" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.Lovisis.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.provider.Telephony.CB_RECEIVED" />

            </intent-filter>
        </activity>
        <activity android:name="com.example.Lovisis.AlarmListner" />
        <activity android:name="com.example.Lovisis.Help" />
        <activity android:name="com.example.Lovisis.Viewdb" />
        <activity android:name="com.example.Lovisis.Sms" />
        <activity android:name="com.example.Lovisis.CbReceiver" />
        <activity android:name="com.example.Lovisis.AlarmInput" />
        <activity android:name="com.example.Lovisis.Manual" />
        <activity
            android:name="com.example.Lovisis.AlarmAlert"
            android:label="@string/title_activity_alarm_alert" >
        </activity>
        <activity android:name="com.example.Lovisis.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  </application>

</manifest>


Logcat:

**04-17 15:14:26.599: D/AndroidRuntime(3024): Shutting down VM
04-17 15:14:26.599: W/dalvikvm(3024): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-17 15:14:26.599: E/AndroidRuntime(3024): FATAL EXCEPTION: main
04-17 15:14:26.599: E/AndroidRuntime(3024): java.lang.NullPointerException
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.example.Lovisis.MainActivity.applyRotation(MainActivity.java:112)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.example.Lovisis.MainActivity.gotoScreen1(MainActivity.java:88)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.example.Lovisis.MainActivity.access$0(MainActivity.java:86)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.example.Lovisis.MainActivity$1.onClick(MainActivity.java:29)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.view.View.performClick(View.java:2485)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.view.View$PerformClick.run(View.java:9080)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.os.Handler.handleCallback(Handler.java:587)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.os.Looper.loop(Looper.java:130)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at java.lang.reflect.Method.invoke(Method.java:507)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-17 15:14:26.599: E/AndroidRuntime(3024):     at    dalvik.system.NativeStart.main(Native Method)**


我试图在上面给定的活动中发生的单击按钮的活动之间切换,它工作正常,突然开始引发空指针异常。请帮帮我。

最佳答案

只是猜测而已

(View)findViewById( R.id.main);


可能会在将其传递给null之前返回applyRotation,然后返回line 112

final float centerX = container.getWidth() / 2.0f;


会抛出NullPointerException

仔细检查所有涉及的活动中是否存在ID为main的视图。

关于android - 在 Activity 之间切换导致空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16068140/

10-10 01:49