Hi,
I'm not sure you need to use "startActivityForResult", start with
"startActivty like that :
Intent intent = new Intent(NextActivity.this, Hello_Android.class);
startActivity(intent);
Another problem I think is that you need to :
implements View.OnClickListener
to make it work.
To this, maybe it will help...
On 10 fév, 00:29, 안드로이드카이로 <[email protected]> wrote:
> You write setcontentview before setonclicklistener.
>
> Http://www.androidside.com
>
> 2010. 2. 10. 오전 7:46에 "Anthony Westover" <[email protected]>님이 작성:
>
> I am new to android development, experienced with Java. I am trying,
> basically, to play around with OnClick events, using a button click to
> change Layouts. I started by making two .xml files in my Layouts
> folder of the project and just setting setContentView to the first xml
> in onCreate and to the other xml in onClick. Through some searching I
> found that another Activity and an Intent object. I tried this with
> the following:
>
> package org.Android.HelloAndroid;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.os.Bundle;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.Button;
>
> public class NextActivity extends Activity{
>
> public void onCreate(Bundle bundle){
> super.onCreate(bundle);
> Button button = (Button) findViewById(R.id.goback);
> button.setOnClickListener(new OnClickListener(){
> public void onClick(View v){
> Intent myIntent = new Intent(v.getContext(),
> Hello_Android.class);
> startActivityForResult(myIntent,0);
> }
> });
> setContentView(R.layout.onclick);
> }
>
> }
>
> package org.Android.HelloAndroid;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.os.Bundle;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.*;
>
> public class Hello_Android extends Activity{
> /** Called when the activity is first created. */
> �...@override
> public void onCreate(Bundle created) {
> super.onCreate(created);
> TextView tv = (TextView) findViewById(R.id.textview);
> Button button = (Button) findViewById(R.id.button);
> button.setOnClickListener(new OnClickListener(){
> public void onClick(View v){
> Intent myIntent = new
> Intent(v.getContext(),NextActivity.class);
> startActivityForResult(myIntent,0);
> }
> });
> this.setContentView(R.layout.main);
> }
>
> }
>
> When I run this I get the error:
>
> "The application Hello_Android (process org.android.HelloAndroid) has
> stopped unexpectedly. Please try again. Force Close."
>
> I used logcat, which I will be the first to admit I am unfamiliar with
> how to read, but what stood out was the java Stack trace,
>
> E/AndroidRuntime( 180): Uncaught handler: thread main exiting due to
> uncaught exception
>
> E/AndroidRuntime( 180): java.lang.RuntimeException: Unable to start
> activity ComponentInfo{org.Android.HelloAndroid/
> org.Android.HelloAndroid.Hello_Android}:
> java.lang.NullPointerException
>
> E/AndroidRuntime( 180): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2496)
>
> E/AndroidRuntime( 180): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2512)
>
> E/AndroidRuntime( 180): at android.app.ActivityThread.access
> $2200(ActivityThread.java:119)
>
> E/AndroidRuntime( 180): at android.app.ActivityThread
> $H.handleMessage(ActivityThread.java:1863)
>
> E/AndroidRuntime( 180): at
> android.os.Handler.dispatchMessage(Handler.java:99)
>
> E/AndroidRuntime( 180): at android.os.Looper.loop(Looper.java:123)
>
> E/AndroidRuntime( 180): at
> android.app.ActivityThread.main(ActivityThread.java:4363)
>
> E/AndroidRuntime( 180): at
> java.lang.reflect.Method.invokeNative(Native Method)
>
> E/AndroidRuntime( 180): at
> java.lang.reflect.Method.invoke(Method.java:521)
>
> E/AndroidRuntime( 180): at com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:860)
>
> E/AndroidRuntime( 180): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>
> E/AndroidRuntime( 180): at dalvik.system.NativeStart.main(Native
> Method)
>
> E/AndroidRuntime( 180): Caused by: java.lang.NullPointerException
>
> E/AndroidRuntime( 180): at
> org.Android.HelloAndroid.Hello_Android.onCreate(Hello_Android.java:17)
>
> E/AndroidRuntime( 180): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1047)
>
> E/AndroidRuntime( 180): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2459)
>
> What is the most interesting to me is the null pointer exception at
> line 17 of Hello_Android.java, the line in question being:
>
> button.setOnClickListener(new OnClickListener(){
>
> I am not sure how this is throwing a null pointer exception though...
> any help would be appreciated. Without the setOnClickListener call,
> the application loads fine, the TextView and Button from my xml layout
> file both appear.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
>
> NEW! Try asking and tagging your question on Stack Overflow
> athttp://stackoverflow.com/questions/tagged/android
>
> To unsubscribe from this group, send email to
> [email protected]<android-beginners%[email protected]>
> For more options, visit this group
> athttp://groups.google.com/group/android-beginners?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en