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

时间:2016-10-14 11:45:46

标签: java android

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

  

java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.india.chemistry / com.example.india.chemistry.FifthActivity}:java.lang.NullPointerException:尝试调用虚方法'android.content。在空对象引用上的上下文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) 

请帮帮我。

6 个答案:

答案 0 :(得分:1)

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

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

}

答案 1 :(得分:1)

您无法在getApplicationContext之前调用oncreate(结果为空),因为尚未正确创建活动,因此您需要在执行oncreate后调用此活动或super电话之后。

onCreate的超级电话会为您的活动提取应用程序上下文。

解决方案:

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

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

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

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

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

你的oncreate方法。

答案 4 :(得分:0)

堆栈跟踪的第一行提供详细信息:getApplicationContext()'在空对象引用上。未创建活动,它返回null Context。你在全球制作动画的两个对象。尝试以这种方式编辑代码。

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

}

答案 5 :(得分:0)

您需要在应用程序下的清单文件中添加活动。无论何时创建新活动,都将其添加到清单文件中。

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

希望这有帮助。