intent not working
I am learning the concept of Intent
s and the application I am working on crashes when i try to go from one intent to another,the intent object is fired on onClickListener()
of a button.
Below is the code:
intentdemo.java
: first activity
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 intentdemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1= (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(intentdemo.this,Intentdemo2.class);
startActivity(i);
}
});
}
}
Intentdemo2.java
: Activity 2
package com.test.intent;
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 Intentdemo2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(),intentdemo.class);
startActivity(i);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
pa开发者_运维知识库ckage="com.test.intent"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".intentdemo"
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=".Intentdemo2"></activity>
</application>
</manifest>
main.xml: for first activity
<?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="@string/hello"
/>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
layout.xml: for second activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
<TextView
android:text="@string/text"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
I would appreciate if someone can point me towards my mistake here, first Activity is displayed, When i click on button it gives me the message "the activity has stopped unexpectedly", how should I debug in case the error in activity is not intuitive.
Thanks, Sid
Your second activity is loading its layout from main.xml (setContentView(R.layout.main);
), but is trying find button with id R.id.button2
. There's no such button in the main layout, so attempting to set the click listener is crashing on trying to access null
object.
The fix is simple - change the line to setContentView(R.layout.layout);
. (On a separate note - change the name of that file from layout.xml
to something else)
I'm just guessing here but shouldn't
setContentView(R.layout.main);
be
setContentView(R.layout.layout);
in IntentDemo2
I think youu have just copy and pasted main activity in second...so please change setContentView(R.layout.main);
to appropriate layout.
your first activity,
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 intentdemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1= (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(intentdemo.this,Intentdemo2.class);
startActivity(i);
}
});
}
}
your second activity,
package com.test.intent;
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 Intentdemo2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
精彩评论