繁体   English   中英

启动另一个活动时应用程序崩溃

[英]App crashes when launching another activity

我到处搜索我的问题的解决方案,但找不到。所以,问题是我想通过按钮从我的MainActivity启动另一个名为FifthActivity活动,但我的活动一直崩溃。 我检查了logcat并发现了这个 -

java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.example.india.chemistry/com.example.india.chemistry.FifthActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'android.content.Context android.空对象引用上的 content.Context.getApplicationContext()'

下面是我的 mainActivity java 文件。

package com.example.india.chemistry;

public class MainActivity extends AppCompatActivity {
    private static boolean isRunning = false;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(!isRunning)
        {
            isRunning = true;
            startActivity(new Intent(this, Intro.class));//Play your video here
        }
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
 }

    @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) {
        int id = item.getItemId();
       if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void part_a( View View){
            Intent intent1 = new Intent("com.example.india.chemistry.Second_Activity");
            startActivity(intent1);
    }

    public void part_b( View View) {
        Intent intent2 = new Intent("com.example.india.chemistry.ThirdActivity");
        startActivity(intent2);
    }

    public void formulas( View View) {
        Intent intent4 = new Intent(this,FifthActivity.class);
        startActivity(intent4);
    }    
}

下面是我的FifthActivity.java文件。

package com.example.india.chemistry;


public class FifthActivity extends ActionBarActivity   {
    final Animation shake = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.shake);
    final Animation bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_fifth, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();    

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

        return super.onOptionsItemSelected(item);
    }


        public void nernst(View View) {
            Intent intentf1 = new Intent("com.example.india.chemistry.nernst_activity");
            startActivity(intentf1);
        }

        public void waterhardness(View View) {
            Intent intentf2 = new Intent("com.example.india.chemistry.WaterHardness");
            startActivity(intentf2);

        }

        public void determinationOfCao(View view) {
            Intent intentf3 = new Intent("com.example.india.chemistry.CementCao");
            startActivity(intentf3);
        }

        public void COD_waste_water(View view) {
            Intent intentf4 = new Intent("com.example.india.chemistry.COD");
            startActivity(intentf4);
        }

        public void C_in_B(View view) {
            Intent intentf5 = new Intent("com.example.india.chemistry.Copper_in_Brass");
            startActivity(intentf5);
        }

        public void alkalinity(View view) {
            Intent intentf6 = new Intent("com.example.india.chemistry.Alkalinity");
            startActivity(intentf6);
        }

        public void haematite_ore(View view) {
            Intent intentf7 = new Intent("com.example.india.chemistry.HaemetiteOre");
            startActivity(intentf7);
        }

        public void colourimetry_copper(View view) {
            Intent intentf8 = new Intent("com.example.india.chemistry.Colourimetric_Copper");
            startActivity(intentf8);
        }

        public void potentiometry_fas(View view) {
            Intent intentf9 = new Intent("com.example.india.chemistry.Potentiometry_fas");
            startActivity(intentf9);
        }

        public void conductometry(View view) {
            Intent intentf10 = new Intent("com.example.india.chemistry.Conductometry");
            startActivity(intentf10);
        }

        public void pka(View view) {
            Intent intentf11 = new Intent("com.example.india.chemistry.pka");
            startActivity(intentf11);
        }



}

下面是logcat。

10-14 17:13:58.145 15430-15430/com.example.india.chemistry E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.india.chemistry, PID: 15430 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.india.chemistry/com.example.india.chemistry.FifthActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.india.chemistry.FifthActivity.<init>(FifthActivity.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1085)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5885) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

请帮我。

试试这个,让我知道问题是否解决了.....

Animation shake,bounce;

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

    shake = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

}

您不能在oncreate之前调用getApplicationContext (结果为 null),因为尚未正确创建活动,因此您需要在oncreate执行后或在super call 之后调用它。

onCreate的超级调用为您的活动获取应用程序上下文。

解决方案 :

因此,要么将您的代码放在oncreate要么创建一个函数并在super调用后从oncreate内部调用它。

注意:您必须避免使用final使identifiers全局化,因为使用final的延迟初始化仅适用于constructors但不用担心,当您的活动被创建或重新创建时, onCreate会执行一次。

您需要在清单文件中的 application 下添加活动。 每当您创建新活动时,将其添加到清单文件中。

<application
...
<activity
            android:name=". FifthActivity"
            android:label="Fifth" />
...
</application>

希望这可以帮助。

尝试这个:

public class FifthActivity extends ActionBarActivity   {
    Animation shake,bounce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fifth);
shake = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);
    }

您的视图尚未创建,请移动:

shake = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.shake);
bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);

到您的 oncreate 方法。

堆栈跟踪的第一行给出了详细信息:在空对象引用上的 getApplicationContext()'。意思是没有创建活动,它返回空上下文。 您可以在全局范围内制作两个动画的对象。 尝试以这种方式编辑您的代码。

public class FifthActivity extends ActionBarActivity   {

Animation shake,bounce; 

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

    shake = AnimationUtils.loadAnimation(getApplicationContext(),
        R.anim.shake);
    bounce = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.bounce);
} 

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM