java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo java.lang.nullpointerexception newbie code
Je suis très nouveau à la programmation et je suis désolé si ce type de question a été posée des milliers de fois déjà. Je suis vraiment très nouveau à tout cela, et la lecture de code est encore assez confus pour moi.
Donc j'ai suivi quelques tutoriels en ligne et j'ai réussi à créer cette application simple qui ajoute ou soustrait 1 en fonction du bouton appuyé.
J'ai récemment pris comment pour ajouter un écran d'accueil et d'apprentissage des activités et des modes de travail.
J'ai donc essayé d'ajouter un splash art avant mon application démarre, mais j'obtiens une erreur qui bloque l'application après le démarrage de l'art se termine. Je me demandais si vous pouviez m'aider un débutant à l'extérieur. merci à l'avance. cheers!
c'est l'erreur de logcat
09-11 14:02:29.312: D/dalvikvm(3078): GC_FOR_ALLOC freed 62K, 10% free 2676K/2948K, paused 29ms,
total 31ms
09-11 14:02:29.382: I/dalvikvm-heap(3078): Grow heap (frag case) to 12.189MB for 9830416-byte
allocation
09-11 14:02:29.422: D/dalvikvm(3078): GC_FOR_ALLOC freed 2K, 3% free 12274K/12552K, paused 44ms,
total 44ms
09-11 14:02:30.202: D/dalvikvm(3078): GC_FOR_ALLOC freed <1K, 3% free 12274K/12552K, paused
26ms, total 26ms
09-11 14:02:30.313: I/dalvikvm-heap(3078): Grow heap (frag case) to 28.857MB for 17479696-byte
allocation
09-11 14:02:30.432: D/dalvikvm(3078): GC_FOR_ALLOC freed <1K, 1% free 29343K/29624K, paused
117ms, total 117ms
09-11 14:02:31.052: D/gralloc_goldfish(3078): Emulator without GPU emulation detected.
09-11 14:02:34.112: D/AndroidRuntime(3078): Shutting down VM
09-11 14:02:34.124: W/dalvikvm(3078): threadid=1: thread exiting with uncaught exception
(group=0x41465700)
09-11 14:02:34.152: E/AndroidRuntime(3078): FATAL EXCEPTION: main
09-11 14:02:34.152: E/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.testapp.numbercounter/com.testapp.numbercounter.Main}:
java.lang.NullPointerException
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.access$600(ActivityThread.java:141)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.os.Handler.dispatchMessage(Handler.java:99)
09-11 14:02:34.152: E/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:137)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 14:02:34.152: E/AndroidRuntime(3078):at
java.lang.reflect.Method.invokeNative(NativeMethod)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
java.lang.reflect.Method.invoke(Method.java:525)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 14:02:34.152: E/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method)
09-11 14:02:34.152: E/AndroidRuntime(3078): Caused by: java.lang.NullPointerException
09-11 14:02:34.152: E/AndroidRuntime(3078): at android.graphics.PorterDuffColorFilter.<init>
(PorterDuffColorFilter.java:28)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.graphics.drawable.Drawable.setColorFilter(Drawable.java:424)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.testapp.numbercounter.Main.onCreate(Main.java:26)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.Activity.performCreate(Activity.java:5133)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-11 14:02:34.152: E/AndroidRuntime(3078): ... 11 more
09-11 14:02:38.182: I/Process(3078): Sending signal. PID: 3078 SIG: 9
Voici le code à l'Main.class
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView number;
Button add, sub, res;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
number = (TextView) findViewById(R.id.numberTxt);
add = (Button) findViewById(R.id.addBtn);
sub = (Button) findViewById(R.id.subBtn);
res = (Button) findViewById(R.id.resBtn);
add.getBackground().setColorFilter(color.white, null);
sub.getBackground().setColorFilter(color.white, null);
res.getBackground().setColorFilter(color.white, null);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
counter++;
number.setText(Integer.toString(counter));
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
counter--;
number.setText(Integer.toString(counter));
}
});
res.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
counter = 0;
number.setText(Integer.toString(counter));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
et voici le code de la SplashActivity.class
package com.testapp.numbercounter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle SplashBndl) {
//TODO Auto-generated method stub
super.onCreate(SplashBndl);
setContentView(R.layout.splashscreen);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}
finally {
Intent StartPoint = new Intent(SplashActivity.this, Main.class);
SplashActivity.this.startActivity(StartPoint);
}
}
};
timer.start();
}
protected void onPause(){
super.onPause();
finish();
}
}
et enfin mon AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp.numbercounter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity" 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" android:label="@string/app_name" >
<intent-filter>
<action android:name="com.testapp.numbercounter.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
espère que quelqu'un peut aider, j'ai été coincé sur ce problème depuis un certain temps maintenant.
Vous devez vous connecter pour publier un commentaire.
Vous ne pouvez pas fournir un
null
PorterDuff mode (Le deuxième argument) lors de la configuration du filtre de couleurSpécifier un porterduff mode et cela devrait fonctionner aussi longtemps comme tout le reste dans votre code est fonctionnel..
Je ne suis pas sûr de ce que vous essayez d'accomplir avec le colorfilter. Si vous souhaitez spécifier une couleur d'arrière-plan vous pouvez simplement utiliser
supprimer la
<intent-filter
> à partir de la<activity>
tag..Problème n'est pas avec l'activité dans le manifeste. C'est un pointeur null dans cette ligne
vérifier la réponse donnée par dymmeh pour plus d'info.