我正在做一个Android应用,我想在点击“旅行和假期”按钮时在单独的屏幕上显示旅行联系号码列表。这是代码:
package com.example.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class App2Activity extends Activity {
String number=getIntent().getStringExtra("no");
String name=getIntent().getStringExtra("no");
Button button;
Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(context,AppActivity.class);
i.putExtra("no","yourno");
i.putExtra("name","yourname");
startActivity(i);
}
});
return;
}
}
package com.example.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class App2Activity extends Activity {
String number=getIntent().getStringExtra("784568435,438756435,435784365");
String name=getIntent().getStringExtra("Travels1,Travels2,Travels3");
Button button;
Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(context,AppActivity.class);
i.putExtra("number","784568435,438756435,435784365");
i.putExtra("name","Travels1,Travels2,Travels3");
startActivity(i);
}
});
return;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Travels and Holidays" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Travels and Holidays Details</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".App2Activity" >
</activity>
</application>
</manifest>
我想要的是:当我点击第一个屏幕中的“旅行和假期”按钮时,我希望旅行名称和联系号码显示在另一个屏幕上。点击任何旅行时,它应该重定向到该号码的呼叫选项。
我哪里错了?请帮忙。提前致谢
答案 0 :(得分:0)
您应该只在getIntent()
内使用onCreate()
。
答案 1 :(得分:0)
在初始化Activity时,不应该在Activity的onCreate()方法上getIntent()
。因为实例变量初始化实际上是由编译器放在构造函数中的,所以我认为意图没有在构造函数中初始化。
public class App2Activity extends Activity {
String number;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
number = getIntent().getStringExtra("no");
}
}
答案 2 :(得分:0)
在你的按钮监听器中,你必须调用App2Activity
,同时注意上下文中的拼写错误,
App2Activity.class
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(context,App2Activity.class);
// ↑: number 2 missing!!
// note you're harcoding the no and name variables!
i.putExtra("no","yourno");
i.putExtra("name","yourname");
startActivity(i);
}
});
之后,
App2Activity
中的类似代码(无需显示内容)label
或TextView
制作布局以显示信息oncreate
中的获得额外内容:
App2Activity
答案 3 :(得分:0)
您需要在onCreate()方法中获得意图。此外,在获取意图值时,您将使用相同的密钥&#34; no&#34;两次。您将在数字和名称中获得相同的值。这样做
String number=getIntent().getStringExtra("no");
String name=getIntent().getStringExtra("yourname");
AppActivity中的onCreate()方法应如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(context,AppActivity.class);
i.putExtra("number","784568435,438756435,435784365");
i.putExtra("name","Travels1,Travels2,Travels3");
startActivity(i);
}
});
return;
}
然后从onCreate()方法上面删除getIntent()代码。