web-dev-qa-db-fra.com

Application Android incapable de démarrer l'activité composantinfo

Je suis un nouveau programmeur Android et récemment, beaucoup de mes projets ont eu cette erreur:

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

Quelqu'un peut-il m'aider à résoudre ce problème?

Voici la trace entière de la pile logcat:

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

Voici la 1ère activité: 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;

            }


        }


}

La deuxième activité: 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(); 

        }
    });
}
}

Enfin, mon fichier manifeste: 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>

Toute aide est très appréciée, sans résoudre ce problème, je ne peux pas tester mon application!

45
Slicekick

Votre exception de pointeur null semble être sur cette ligne:

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

car intent.getExtras() renvoie la valeur null lorsque l'intention ne comporte pas de suppléments.

Vous devez réaliser que ce morceau de code:

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

ne démarre pas l'activité que vous avez écrite dans Main.Java, il tentera de démarrer une activité appelée ToClass et si elle n'existe pas, votre application se bloque.

De plus, comme "Android.intent.action.start" n'existe pas, le manifeste devrait ressembler davantage à:

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

J'espère que cela corrige certains des problèmes que vous rencontrez, mais je vous suggère fortement de consulter quelques tutoriels de démarrage pour le développement Android et de les développer à partir de là.

27
Mircea Nistor

On a déjà répondu à la question, mais je veux ajouter plus d'informations sur les causes.

Application Android incapable de démarrer l'activité composantinfo

Cette erreur est souvent associée aux journaux appropriés. Vous pouvez lire les journaux et résoudre ce problème facilement.

Voici un exemple de journal. Dans lequel vous pouvez voir clairement ClassCastException. Ce problème est donc dû au fait que TextView ne peut pas être converti en EditText.

Causée par: Java.lang.ClassCastException: Android.widget.TextView ne peut pas être converti en 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

Quelques erreurs communes.

1 .findViewById() de vue non existante

Comme lorsque vous utilisez findViewById(R.id.button) lorsque le numéro d'identification du bouton n'existe pas dans XML de présentation.

2. Mauvais casting.

Si vous trompez une classe, vous obtenez alors cette erreur. De la même façon que vous transposez RelativeLayout en LinearLayout ou EditText en TextView

3. Activité non inscrite dans manifest.xml

Si vous n'avez pas enregistré d'activité dans manifest.xml, cette erreur survient.

4. findViewById() avec déclaration au plus haut niveau

Le code ci-dessous est incorrect. Cela va créer une erreur. Parce que vous devriez faire findViewById() après avoir appelé setContentView(). Parce qu'une vue peut être présente après sa création.

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. Démarrer abstract Classe d'activité.

Lorsque vous essayez de démarrer une activité qui est abstraite, vous obtiendrez cette erreur. Donc, juste removeabstractkeyword avant le nom de la classe d'activité.

6. Utilisation de kotlin mais kotlin non configuré.

Si votre activité est écrite en Kotlin et que vous n'avez pas configuré kotlin dans votre application. alors vous obtiendrez une erreur. Vous pouvez suivre des étapes simples comme indiqué dans Android Link ou Kotlin Link . Vous pouvez vérifier cette réponse aussi.

Plus d'information

Lisez à propos de Downcast et Upcast

En savoir plus sur findViewById(), méthode de la classe d'activité

Super mot clé en Java

Comment enregistrer une activité dans un manifeste

1
Khemraj