2010-11-16 12 views
0

intentTest.java:mon application se bloque avant d'starts.This est une application simple dans lequel je fais l'utilisation de l'intention d'appeler une autre classe d'une classe précédente

package intentTest.xyz.com; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 

     public class intentTest extends Activity { 
     Button b; 
      /** Called when the activity is first created. */ 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     b = (Button) findViewById(R.id.b); 

     b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
      Intent intent = new Intent(intentTest.this,seond.class); 
      startActivity(intent); 

      } 

     }); 

    } 
} 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="First screen" 
     /> 

    <Button 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="click" 
    android:name="@+id/b" 
    /> 
    </LinearLayout> 

seond.java:

package intentTest.xyz.com; 

import android.app.Activity; 
import android.os.Bundle; 

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

    } 
} 

second.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 


    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="second screen" 
    /> 

manifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="intentTest.xyz.com" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".intentTest" 
       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=".seond"></activity> 
</application> 


</manifest> 

i obtenir le code d'erreur suivant:

11-16 01:26:07.156: ERROR/AndroidRuntime(779): Uncaught handler: thread main exiting due to uncaught exception 
11-16 01:26:07.166: ERROR/AndroidRuntime(779): java.lang.RuntimeException: Unable to start activity ComponentInfo{intentTest.xyz.com/intentTest.xyz.com.intentTest}: java.lang.NullPointerException 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread.access$2100(ActivityThread.java:116) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.os.Looper.loop(Looper.java:123) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at dalvik.system.NativeStart.main(Native Method) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779): Caused by: java.lang.NullPointerException 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at intentTest.xyz.com.intentTest.onCreate(intentTest.java:18) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
11-16 01:26:07.166: ERROR/AndroidRuntime(779):  ... 11 more 

Répondre

3

Regardez le fichier main.xml, vous avez fait une petite erreur qui conduit à beaucoup de discussion parfois.

android:name="@+id/b"

il devrait être comme:

android:id="@+id/b"

Pour que vous puissiez accéder à cet élément dans votre activité et c'est la raison pour laquelle votre application provoque

NullPointerException 

Par le De cette façon, vous utilisez des lettres minuscules dans le nom de la classe et des lettres majuscules dans le nom du paquet, ce qui n'est pas une bonne pratique pour les codeurs.

+0

merci ... qui était très utile de vous – heha

+1

La réponse de Steve était également correcte mais vous n'avez pas prêté attention à cette réponse. S'il vous plaît essayez de faire au moins un vote pour des réponses utiles. –

+0

Vikas, merci pour votre commentaire! C'est gentil de votre part! – Steve

0

ajouter à manifest.xml

<activity android:name=".seond" 
      android:label="@string/app_name"> 
</activity> 
+0

ne fonctionne pas ... ne peut pas ajouter un '*' – heha

+0

sans '*' :)))) – user479211

+0

essayé ... il ne marche pas encore wrk – heha

1

Vous manquez orthographié secondes .class ligne 18 ..vous doit être endormi droit?

+0

son est seond ... – heha

1
<activity android:name=".seond" 
      android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

écrire ce code à U'r manifeste

2

Le problème est l'id du bouton. L'ID est défini comme android:name et non comme android:id. Remplacez dans le main.xml la partie du bouton avec l'extrait de code suivant et cela devrait fonctionner.

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="click" 
    android:id="@+id/b" 
    /> 

Le NullpointerExcpetion a été causé, parce b dans votre intentTest était nulle.