E / AndroidRuntime:致命异常:主要

时间:2015-10-14 07:32:08

标签: java android runtime-error

在我修改这些点击侦听器之后出现了这个错误

10-14 12:22:47.811 14626-14626/? E/AndroidRuntime: FATAL EXCEPTION: main
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime: Process: com.thebubble.animationstransitions, PID: 14626
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thebubble.animationstransitions/com.thebubble.animationstransitions.MainActivity}: java.lang.NullPointerException
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:154)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5315)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at com.thebubble.animationstransitions.MainActivity.onCreate(MainActivity.java:28)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5283)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:154) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5315) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
10-14 12:22:47.811 14626-14626/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
10-14 12:22:47.827 14626-14626/? I/Process: Sending signal. PID: 14626 SIG: 9

MainActivity.java代码

package com.thebubble.animationstransitions;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity {
    int touchCount;

    Button theButton;
    ViewGroup mainView;


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


        theButton.setOnClickListener(
                new Button.OnClickListener(){
                    boolean bull = true;


                    public void onClick(View v){
                        moveButton(bull);
                        if(bull==true){
                            bull = false;
                        }else{
                            bull =true;
                        }

                    }


                }
        );
    }
    public void moveButton(boolean decision) {

        if (decision) {


            View theButton = findViewById(R.id.theButton);

            TransitionManager.beginDelayedTransition(mainView);


            //Changes the position of the button
            RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            theButton.setLayoutParams(positionRules);


            ViewGroup.LayoutParams sizeRules = theButton.getLayoutParams();
            sizeRules.width = 450;
            sizeRules.height = 450;
            theButton.setLayoutParams(sizeRules);
        } else {


            View theButton = findViewById(R.id.theButton);

            TransitionManager.beginDelayedTransition(mainView);


            //Changes the position of the button
            RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            positionRules.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            positionRules.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            theButton.setLayoutParams(positionRules);


            ViewGroup.LayoutParams sizeRules = theButton.getLayoutParams();
            sizeRules.width = 150;
            sizeRules.height = 50;
            theButton.setLayoutParams(sizeRules);
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

它只是一个简单的代码,可以更改按钮的位置和大小,并创建一个简单的过渡 我是初学者,所以我需要更详细的答案

1 个答案:

答案 0 :(得分:2)

您忘记初始化按钮theButton

插入:

theButton = (Button) findViewById(R.id.yourButtonId);

在设置点击监听器之前。