在导致空指针异常的活动之间切换

时间:2013-04-17 19:06:57

标签: android nullpointerexception android-activity android-animation

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)**

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

2 个答案:

答案 0 :(得分:1)

猜猜,但是

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

可以在将null传递给applyRotation,然后line 112

之前返回final float centerX = container.getWidth() / 2.0f;
NullPointerException

会抛出main

仔细检查所有相关活动中是否存在ID为{{1}}的视图。

答案 1 :(得分:0)

很抱歉,实际上我为两个布局定义了相同的名称,导致空指针异常,无论如何感谢您的支持..