开发者

my app crashes before it starts.This is a simple app in which i am making the use of intent to call another class from a previous class

intentTest.java:

    package intentTest.xyz.com;

     import android.app.Activity;
   import android.content.Intent;
   import android.os.Bundle;
  import android.view.View;
  import an开发者_开发问答droid.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 get the following error code:

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


Look at the main.xml file, you have made a little mistake that leads to a lot of discussion sometimes.

android:name="@+id/b"

it should be like:

android:id="@+id/b"

So that you can access this element in your activity and that's why your application causing

NullPointerException 

By the way you are using small letters in class name and capital letters in package name which is not a good practice for coders.


The problem is the button's id. The id is defined as android:name and not as android:id. Replace in the main.xml the button part with following code snippet and it should work.

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

The NullpointerExcpetion was caused, because b in your intentTest was null.


You miss spelled second.class line 18 ..you must be asleep right?


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

write this code to u'r manifest


add to Manifest.xml

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜