Android Studio中的启动画面问题:错误"不幸的是MyApplication已停止",java.lang.NullPointerException错误

时间:2016-02-04 05:21:12

标签: android splash-screen

我正在运行启动画面5秒钟。一旦时间结束,而不是去MainActivity.java,我的应用程序终止并给我一个错误说"不幸的是我的应用程序已停止"。

请查看下面的LogCat。

我也在想我可能在AndroidManifest.xml中编写了动作android:name和category android:name。

我非常擅长编程,并且正在学习Eclipse教程中的Android Studio。我真的很感谢详细的指导。

以下是我的代码。

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.example.android.myapplication.MainActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.example.android.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    int counter = 0;
    Button add = (Button) findViewById(R.id.bAdd);
    Button sub = (Button) findViewById(R.id.bSub);
    TextView display = (TextView) findViewById(R.id.tView);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });





        add.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){

                counter++;
                display.setText("Your total is " + counter);

            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                counter = counter - 1;
                display.setText("Your total is " + counter);

            }
        });




    }

    @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);
    }
}

Splash.xml

package com.example.android.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by Psp on 2016-02-02.
 */
public class Splash extends Activity {


    @Override
    protected void onCreate(Bundle PLovesMishti) {
        super.onCreate(PLovesMishti);
        setContentView(R.layout.splash);

        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(5000);

                } catch (InterruptedException e) {
                    e.printStackTrace();

                } finally {
                    Intent openStartingPoint = new Intent("com.example.android.myapplication.MainActivity");
                    startActivity(openStartingPoint);

                }
            }
        };
        timer.start();
    }
}

logcat的

02-03 23:53:45.920: E/AndroidRuntime(4264): ***FATAL EXCEPTION: main***

02-03 23:53:45.920: E/AndroidRuntime(4264): Process: com.example.android.myapplication, PID: 4264

02-03 23:53:45.920: E/AndroidRuntime(4264): ***java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.myapplication/com.example.android.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference***

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread.access$800(ActivityThread.java:144)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.os.Handler.dispatchMessage(Handler.java:102)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.os.Looper.loop(Looper.java:135)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread.main(ActivityThread.java:5221)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at java.lang.reflect.Method.invoke(Native Method)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at java.lang.reflect.Method.invoke(Method.java:372)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

02-03 23:53:45.920: E/AndroidRuntime(4264): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.Activity.findViewById(Activity.java:2071)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at com.example.android.myapplication.MainActivity.<init>(MainActivity.java:18)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at java.lang.reflect.Constructor.newInstance(Native Method)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at java.lang.Class.newInstance(Class.java:1572)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.Instrumentation.newActivity(Instrumentation.java:1065)

02-03 23:53:45.920: E/AndroidRuntime(4264):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)

6 个答案:

答案 0 :(得分:1)

更改行

Intent openStartingPoint = new Intent("com.example.android.myapplication.MainActivity");
startActivity(openStartingPoint);

Intent openStartingPoint = new Intent(Splash.this, MainActivity.class);
startActivity(openStartingPoint);

您在活动实例化之前获得了视图

Button add = (Button) findViewById(R.id.bAdd);
Button sub = (Button) findViewById(R.id.bSub);
TextView display = (TextView) findViewById(R.id.tView);

从全局声明中删除这些行,并在onCreate()之后将其添加到MainActivity setContentView(R.layout.activity_main)

public void onCreate() {
    ....
    setContentView(R.layout.activity_main);
    ....
    Button add = (Button) findViewById(R.id.bAdd);
    Button sub = (Button) findViewById(R.id.bSub);
    TextView display = (TextView) findViewById(R.id.tView);
}

答案 1 :(得分:1)

试试这个。问题是你的AndroidMenifest.xml代码只删除了一些代码。

<intent-filter>
    <action android:name="com.example.android.myapplication.MainActivity" />

    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

在MainActivity.java文件中更改以下内容 -

Intent openStartingPoint = new Intent("com.example.android.myapplication.MainActivity");
startActivity(openStartingPoint);

到下面给出的代码 -

Intent openStartingPoints = new Intent(SplashActivity.this,MainActivity.class);
startActivity(openStartingPoints);

答案 2 :(得分:0)

更改行:

 Intent openStartingPoint = new Intent("com.example.android.myapplication.MainActivity");
    startActivity(openStartingPoint);

Intent openStartingPoint = new Intent(SplashActivity.this,MainActivity.class);
startActivity(openStartingPoint);

它可能对你有帮助。

答案 3 :(得分:0)

使用此行并将主要活动粘贴到空指针例外,因为初始化主活动视图时您似乎会出错。

Intent i = new Intent(SplashActivity.this,MainActivity.class); startActivity(ⅰ);

答案 4 :(得分:0)

日志猫说你指向一个空对象基本上意味着计算机看到对象,但它的值为0,这意味着它不存在,或者它没有数据可能想检查你尝试的位置拉出spash.xml你的问题是在主活动中,还是在splash.xml上你有time.start();没有停止点巫婆可以让它崩溃,因为你在一个永无止境的循环

答案 5 :(得分:0)

从finally块

替换行
Intent openStartingPoint = new Intent("com.example.android.myapplication.MainActivity");
    startActivity(openStartingPoint);

to 


 Intent openStartingPoint = new Intent(Splash.this, MainActivity.java);
    startActivity(openStartingPoint);

主要活动应该是

public class MainActivity extends AppCompatActivity {


        int counter = 0;
        Button add; // Declare here 
        Button sub ;
        TextView display;


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

    // Initialize here after setContentView

         add = (Button) findViewById(R.id.bAdd);
         sub = (Button) findViewById(R.id.bSub);
         display = (TextView) findViewById(R.id.tView);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });





            add.setOnClickListener(new View.OnClickListener(){

                public void onClick(View v){

                    counter++;
                    display.setText("Your total is " + counter);

                }
            });

            sub.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    counter = counter - 1;
                    display.setText("Your total is " + counter);

                }
            });




        }

        @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);
        }
    }