The application hangs when launching – Unable to instantiate the ComponentInfo activity &lb…

I’m new to android development and java programming. I have been following classes on Udacity with no problem, so I decided to start trying to create my first app on my own. It is a tip calculator. My app never opens when I try to run it in both the emulator and my Galaxy S6, it simply crashes right away. I’ve tried to review the logs and search through past threads on StackOverflow and other sites, but no solution has worked so far and I am absolutely lost. Can anyone please help me. I’ve only written java corresponding to the button that would display a 10% tip so far and ran into this error while testing it. I’ve attached my MainActivity.java, activity_main.xml and logcat below. I also added my AndroidManifest.xml as this seemed to be where most other people had a similar problem, but as I said I could not get any past solution to work. Any help would be very much appreciated! Thanks!

activity_main.xml





    

    





    

    

MainActivity.java

package com.example.android.tipcalculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

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

EditText checkTotal = (EditText) findViewById(R.id.check_total);
String checkTotalValue = checkTotal.getText().toString();

double totalCheck = Double.parseDouble(checkTotalValue);

public void calcTenPercent(View view) {

    double tenPercent = 0.10;

    displayTip(totalCheck * tenPercent);

}

private void displayTip(double number) {

    TextView finalTip = (TextView) findViewById(R.id.final_tip);
    finalTip.setText(NumberFormat.getCurrencyInstance().format(number));
}
}

AndroidManifest.xml



    
        
            

            
        
    


logcat

07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator E/Zygote﹕ MountEmulatedStorage()
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator E/Zygote﹕ v2
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/libpersona﹕ KNOX_SDCARD checking this for 10243
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/libpersona﹕ KNOX_SDCARD not a persona
    07-27 20:59:13.331  23270-23270/com.example.android.tipcalculator I/SELinux﹕ Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SAMSUNG-SM-G920A_5.0.2_0011
    07-27 20:59:13.341  23270-23270/com.example.android.tipcalculator E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
    07-27 20:59:13.341  23270-23270/com.example.android.tipcalculator I/art﹕ Late-enabling -Xcheck:jni
    07-27 20:59:13.351  23270-23270/com.example.android.tipcalculator D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
    07-27 20:59:13.351  23270-23270/com.example.android.tipcalculator D/ActivityThread﹕ Added TimaKeyStore provider
    07-27 20:59:13.381  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Inside getClassLibPath + mLibMap{0=, 1=}
    07-27 20:59:13.381  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Inside getClassLibPath caller
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator D/InjectionManager﹕ InjectionManager
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator D/InjectionManager﹕ fillFeatureStoreMap com.example.android.tipcalculator
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ Constructor com.example.android.tipcalculator, Feature store :{}
    07-27 20:59:13.391  23270-23270/com.example.android.tipcalculator I/InjectionManager﹕ featureStore :{}
    07-27 20:59:13.411  23270-23270/com.example.android.tipcalculator D/AndroidRuntime﹕ Shutting down VM
    07-27 20:59:13.411  23270-23270/com.example.android.tipcalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.android.tipcalculator, PID: 23270
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.tipcalculator/com.example.android.tipcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                at android.app.Activity.findViewById(Activity.java:2197)
                at com.example.android.tipcalculator.MainActivity.(MainActivity.java:20)
                at java.lang.reflect.Constructor.newInstance(Native Method)
                at java.lang.Class.newInstance(Class.java:1650)
                at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
                at android.app.ActivityThread.access$900(ActivityThread.java:182)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:145)
                at android.app.ActivityThread.main(ActivityThread.java:6141)
                at java.lang.reflect.Method.invoke(Native Method)`enter code here`
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

UPDATED LOGCAT

The App now runs, but seems to terminate during the onClick. I will be working to resolve this issue and start a separate question should the need arise.

07-27 23:31:04.481  10846-10846/com.example.android.tipcalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.tipcalculator, PID: 10846
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4273)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4268)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
 Caused by: java.lang.NumberFormatException: Invalid double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:267)
        at java.lang.Double.parseDouble(Double.java:301)
        at com.example.android.tipcalculator.MainActivity.calcTenPercent(MainActivity.java:30)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4268)
        at android.view.View.performClick(View.java:5217)
        at android.view.View$PerformClick.run(View.java:20983)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

The problem is here :

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

EditText checkTotal = (EditText) findViewById(R.id.check_total);
String checkTotalValue = checkTotal.getText().toString();

Don’t call findViewById in the global variable initialization which will called before the onCreate so the Activity still have no Content View (Which you will set on the onCreate).

So you can change your code to :

EditText checkTotal;
String checkTotalValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkTotal = (EditText) findViewById(R.id.check_total);
    checkTotalValue = checkTotal.getText().toString();
}
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » The application hangs when launching – Unable to instantiate the ComponentInfo activity &lb…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录