57

I'm a new Android programmer and recently, a lot of my projects have been getting this error:

07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to start     

activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}: 

java.lang.NullPointerException

Can anyone help me solve this problem?

Here's the entire logcat stack trace:

07-31 23:45:18.512: INFO/ActivityManager(63): Starting activity: Intent {     
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000   
cmp=com.amrit.musifind/.Main }
07-31 23:45:18.682: INFO/ActivityManager(63): Start proc com.amrit.musifind for activity   
com.amrit.musifind/.Main: pid=716 uid=10025 gids={3003}
07-31 23:45:18.962: INFO/jdwp(716): received file descriptor 20 from ADB
07-31 23:45:19.102: DEBUG/ddm-heap(716): Got feature list request
07-31 23:45:19.572: DEBUG/AndroidRuntime(716): Shutting down VM
07-31 23:45:19.572: WARN/dalvikvm(716): threadid=3: thread exiting with uncaught   
exception (group=0x4001aa28)
07-31 23:45:19.572: ERROR/AndroidRuntime(716): Uncaught handler: thread main exiting due 
to uncaught exception
07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to    
start activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}:    
java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
android.app.ActivityThread.access$2100(ActivityThread.java:116)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.os.Handler.dispatchMessage(Handler.java:99)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at       
android.os.Looper.loop(Looper.java:123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
android.app.ActivityThread.main(ActivityThread.java:4203)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at    
java.lang.reflect.Method.invokeNative(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
java.lang.reflect.Method.invoke(Method.java:521)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
dalvik.system.NativeStart.main(Native Method)
07-31 23:45:19.592: ERROR/AndroidRuntime(716): Caused by: java.lang.NullPointerException
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at   
com.amrit.musifind.Main.onCreate(Main.java:44)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
07-31 23:45:19.592: ERROR/AndroidRuntime(716):     ... 11 more
07-31 23:45:19.612: INFO/Process(63): Sending signal. PID: 716 SIG: 3
07-31 23:45:19.612: INFO/dalvikvm(716): threadid=7: reacting to signal 3
07-31 23:45:19.642: INFO/dalvikvm(716): Wrote stack trace to '/data/anr/traces.txt'
07-31 23:45:21.422: INFO/Process(716): Sending signal. PID: 716 SIG: 9
07-31 23:45:21.442: INFO/ActivityManager(63): Process com.amrit.musifind (pid 716) has   
died.
07-31 23:45:21.583: WARN/UsageStats(63): Unexpected resume of com.android.launcher while    
already resumed in com.amrit.musifind
07-31 23:45:21.712: WARN/InputManagerService(63): Window already focused, ignoring focus    
gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@439559d0
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): >>>>>>>>>>>>>> AndroidRuntime START    
<<<<<<<<<<<<<<
07-31 23:45:23.482: DEBUG/AndroidRuntime(725): CheckJNI is ON
07-31 23:45:23.812: DEBUG/AndroidRuntime(725): --- registering native functions ---
07-31 23:45:23.832: INFO/jdwp(725): received file descriptor 20 from ADB
07-31 23:45:24.193: DEBUG/ddm-heap(725): Got feature list request
07-31 23:45:25.052: DEBUG/AndroidRuntime(725): Shutting down VM
07-31 23:45:25.052: DEBUG/dalvikvm(725): DestroyJavaVM waiting for non-daemon threads to   
exit
07-31 23:45:25.062: DEBUG/dalvikvm(725): DestroyJavaVM shutting VM down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread shutting down
07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread has shut down
07-31 23:45:25.073: DEBUG/jdwp(725): JDWP shutting down net...
07-31 23:45:25.073: DEBUG/jdwp(725): +++ peer disconnected
07-31 23:45:25.073: INFO/dalvikvm(725): Debugger has detached; object registry had 1   
entries
07-31 23:45:25.093: DEBUG/dalvikvm(725): VM cleaning up
07-31 23:45:25.113: DEBUG/dalvikvm(725): LinearAlloc 0x0 used 665652 of 4194304 (15%)
07-31 23:47:22.712: DEBUG/dalvikvm(104): GC freed 9173 objects / 524184 bytes in 149ms

Heres the 1st activity: start.java

package com.amrit.musifind;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.amrit.musifind.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public abstract class start extends Activity implements OnClickListener {

Button buttonGo, buttonReset;
EditText EditTextinput;
String input = "";
String ur = "http://www.tastekid.com/ask/ws?q=";
String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON";
String url = "" ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonGo = (Button) findViewById(R.id.buttonGo);
    buttonReset = (Button) findViewById(R.id.buttonReset);

    EditTextinput = (EditText) findViewById(R.id.EditTextinput);

    //Button listener
    buttonReset.setOnClickListener(this);
    buttonGo.setOnClickListener(this);
}

public void onClick (View src){
    switch(src.getId()){
        case R.id.buttonGo:
            input = EditTextinput.getText().toString();
            url = ur + input + l  ;

            Intent Main = new Intent(this, ToClass.class);
            Main.putExtra("userurl", url);
            startActivity(Main);
            break;

        case R.id.buttonReset:
            EditTextinput.setText("");
            break;
        }
    }
}

The second activity: Main.java

package com.amrit.musifind;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.amrit.musifind.JSONfunctions;
import com.amrit.musifind.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    Intent intent = getIntent();
    String url = intent.getExtras().getString("userurl");

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>    
    ();

    JSONObject json = JSONfunctions.getJSONfromURL(url);

    try{
        JSONObject earthquakes = json.getJSONObject("Similar");
        JSONArray info = earthquakes.getJSONArray("Results");

        for (int i = 0; i < info.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = info.getJSONObject(i);

            map.put("id", String.valueOf(i));
            map.put("name", "Name:" + e.getString("Name"));
            map.put("type", "Type: " + e.getString("Type"));
            mylist.add(map);
        }

        JSONArray  results = json.getJSONArray("Results");

        for(int i=0;i<results.length();i++){                        
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = results.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("name", "Name:" + e.getString("name"));
            map.put("type", "Type: " +  e.getString("type"));
            mylist.add(map);            
        }       
    } catch(JSONException e) {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
         new String[] { "name", "type" }, 
         new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", 
                Toast.LENGTH_SHORT).show(); 
            }
        });
    }
}

Lastly, my manifest file: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amrit.musifind"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".start" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.start" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name= ".Main">
            <intent-filter>
                <action android:name="android.intent.action.Main" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

Any help is much appreciated, without solving this problem I can't test my app out!

gotwo
  • 663
  • 8
  • 16
Slicekick
  • 2,119
  • 5
  • 24
  • 35
  • could you please post the complete stack trace? – Mircea Nistor Aug 01 '11 at 15:26
  • How do I do that? Sorry, I'm very new to android :) – Slicekick Aug 01 '11 at 15:46
  • You can find the call stack in the logcat output. You already included part of it at the beginning of your post (the one with the null pointer exception). – Mircea Nistor Aug 01 '11 at 15:50
  • 1
    I got this when I tried to assign a click listener before setting a content view, so if a view.setOnClickListener appears before the setContentView call to set up the view you will get this error, if your code does that too then just change the order to set the view first then assign the listeners. – Damon Smith Feb 04 '13 at 11:52

2 Answers2

30

Your null pointer exception seems to be on this line:

String url = intent.getExtras().getString("userurl");

because intent.getExtras() returns null when the intent doesn't have any extras.

You have to realize that this piece of code:

Intent Main = new Intent(this, ToClass.class);
Main.putExtra("userurl", url);
startActivity(Main);

doesn't start the activity you wrote in Main.java, it will attempt to start an activity called ToClass and if that doesn't exist, your app crashes.

Also, there is no such thing as "android.intent.action.start" so the manifest should look more like:

<activity android:name=".start" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name= ".Main">
</activity>

I hope this fixes some of the issues you are encountering but I strongly suggest you check out some "getting started" tutorials for android development and build up from there.

Mircea Nistor
  • 3,145
  • 1
  • 26
  • 32
  • I removed the code you pointed out, and edited my manifest file but I still get a force close. This is the new error from logcat: 08-01 12:28:01.522: ERROR/AndroidRuntime(807): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.start}: java.lang.InstantiationException: com.amrit.musifind.start – Slicekick Aug 01 '11 at 17:28
  • again, I'll need more than the first line of the call stack – Mircea Nistor Aug 01 '11 at 17:44
  • 1
    I fixed it, I had "public abstract class" instead of just "public class". Thanks! – Slicekick Aug 01 '11 at 17:48
14

The question is answered already, but I want add more information about the causes.

Android app unable to start activity componentinfo

This error often comes with appropriate logs. You can read logs and can solve this issue easily.

Here is a sample log. In which you can see clearly ClassCastException. So this issue came because TextView cannot be cast to EditText.

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

11-04 01:24:10.403: D/AndroidRuntime(1050): Shutting down VM
11-04 01:24:10.403: W/dalvikvm(1050): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:24:10.543: E/AndroidRuntime(1050): FATAL EXCEPTION: main
11-04 01:24:10.543: E/AndroidRuntime(1050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.os.Looper.loop(Looper.java:137)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at java.lang.reflect.Method.invoke(Method.java:525)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at dalvik.system.NativeStart.main(Native Method)
11-04 01:24:10.543: E/AndroidRuntime(1050): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:24:10.543: E/AndroidRuntime(1050):     at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:45)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.Activity.performCreate(Activity.java:5133)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 01:24:10.543: E/AndroidRuntime(1050):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 01:24:10.543: E/AndroidRuntime(1050):     ... 11 more
11-04 01:29:11.177: I/Process(1050): Sending signal. PID: 1050 SIG: 9
11-04 01:31:32.080: D/AndroidRuntime(1109): Shutting down VM
11-04 01:31:32.080: W/dalvikvm(1109): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 01:31:32.194: E/AndroidRuntime(1109): FATAL EXCEPTION: main
11-04 01:31:32.194: E/AndroidRuntime(1109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.os.Looper.loop(Looper.java:137)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invoke(Method.java:525)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at dalvik.system.NativeStart.main(Native Method)
11-04 01:31:32.194: E/AndroidRuntime(1109): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 01:31:32.194: E/AndroidRuntime(1109):     at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:44)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.Activity.performCreate(Activity.java:5133)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 01:31:32.194: E/AndroidRuntime(1109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 01:31:32.194: E/AndroidRuntime(1109):     ... 11 more
11-04 01:36:33.195: I/Process(1109): Sending signal. PID: 1109 SIG: 9
11-04 02:11:09.684: D/AndroidRuntime(1167): Shutting down VM
11-04 02:11:09.684: W/dalvikvm(1167): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-04 02:11:09.855: E/AndroidRuntime(1167): FATAL EXCEPTION: main
11-04 02:11:09.855: E/AndroidRuntime(1167): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.troysantry.tipcalculator/com.troysantry.tipcalculator.TipCalc}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.os.Looper.loop(Looper.java:137)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at java.lang.reflect.Method.invoke(Method.java:525)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at dalvik.system.NativeStart.main(Native Method)
11-04 02:11:09.855: E/AndroidRuntime(1167): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
11-04 02:11:09.855: E/AndroidRuntime(1167):     at com.troysantry.tipcalculator.TipCalc.onCreate(TipCalc.java:44)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.Activity.performCreate(Activity.java:5133)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 02:11:09.855: E/AndroidRuntime(1167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-04 02:11:09.855: E/AndroidRuntime(1167):     ... 11 more

Some Common Mistakes.

1.findViewById() of non existing view

Like when you use findViewById(R.id.button) when button id does not exist in layout XML.

2. Wrong cast.

If you wrong cast some class, then you get this error. Like you cast RelativeLayout to LinearLayout or EditText to TextView.

3. Activity not registered in manifest.xml

If you did not register Activity in manifest.xml then this error comes.

4. findViewById() with declaration at top level

Below code is incorrect. This will create error. Because you should do findViewById() after calling setContentView(). Because an View can be there after it is created.

public class MainActivity extends Activity {

  ImageView mainImage = (ImageView) findViewById(R.id.imageViewMain); //incorrect way

  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainImage = (ImageView) findViewById(R.id.imageViewMain); //correct way
    //...
  }
}

5. Starting abstract Activity class.

When you try to start an Activity which is abstract, you will will get this error. So just remove abstract keyword before activity class name.

6. Using kotlin but kotlin not configured.

If your activity is written in Kotlin and you have not setup kotlin in your app. then you will get error. You can follow simple steps as written in Android Link or Kotlin Link. You can check this answer too.

More information

Read about Downcast and Upcast

Read about findViewById() method of Activity class

Super keyword in Java

How to register activity in manifest

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Another reason could be, if `resources.updateConfiguration()` is called without attaching `super.attachBaseContext(..)`. – soshial Nov 19 '19 at 10:38
  • Mine was caused by wrong Intent types. Send String and tried to get Int as an example. – grrigore Dec 12 '19 at 15:57
  • This is a good answer. Very well written. I would like to add, can you add the causes for navigation also. I am adding this nav graph to the fragment tag in the activity. But I am getting the cause as `android.view.InflateException: Binary XML file line #17 .... Error inflating class fragment` – Mohit Ajwani May 12 '21 at 10:29