我有两项活动 第一个有两个按钮 第二个有一个textview
我需要的是什么 当我点击第一个按钮时,第二个活动打开,textview显示(你好) 当我点击第二个按钮时,第二个活动打开,textview显示(你好吗)
这是第一个活动布局
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:scrollbars="none"
tools:context=".DailyPrayersActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
style="@style/Button_Normal"
android:background="@drawable/btn01"
android:onClick="hello"
android:text="@string/hello" />
<Button
android:id="@+id/btn2"
style="@style/Button_Normal"
android:background="@drawable/btn01"
android:onClick="hru"
android:text="@string/hru" />
</LinearLayout>
</ScrollView>
这是第一个活动的java类
package com.daily.morning;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}
public void hello(View v) {
Intent i1 = new Intent(firstActivity.this, SecondActivity.class);
SecondActivity.textview.setText("Hello");
startActivity(i1);
finish();
}
public void hru(View v) {
Intent i2 = new Intent(FirstActivity.this, SecondActivity.class);
SecondActivity.textview.setText("good morning");
startActivity(i2);
finish();
}
}
当我点击第一个按钮或第二个按钮时,应用程序强制关闭
这是logcat
>>09-06 14:26:24.054: E/AndroidRuntime(30993): FATAL EXCEPTION: main
09-06 14:26:24.054: E/AndroidRuntime(30993): java.lang.IllegalStateException: Could not execute method of the activity
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.view.View$1.onClick(View.java:3699)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.view.View.performClick(View.java:4223)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.view.View$PerformClick.run(View.java:17275)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.os.Handler.handleCallback(Handler.java:615)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.os.Handler.dispatchMessage(Handler.java:92)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.os.Looper.loop(Looper.java:137)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-06 14:26:24.054: E/AndroidRuntime(30993): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 14:26:24.054: E/AndroidRuntime(30993): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 14:26:24.054: E/AndroidRuntime(30993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
09-06 14:26:24.054: E/AndroidRuntime(30993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
09-06 14:26:24.054: E/AndroidRuntime(30993): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:103)
09-06 14:26:24.054: E/AndroidRuntime(30993): at dalvik.system.NativeStart.main(Native Method)
09-06 14:26:24.054: E/AndroidRuntime(30993): Caused by: java.lang.reflect.InvocationTargetException
09-06 14:26:24.054: E/AndroidRuntime(30993): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 14:26:24.054: E/AndroidRuntime(30993): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 14:26:24.054: E/AndroidRuntime(30993): at android.view.View$1.onClick(View.java:3694)
09-06 14:26:24.054: E/AndroidRuntime(30993): ... 12 more
09-06 14:26:24.054: E/AndroidRuntime(30993): Caused by: java.lang.NullPointerException
09-06 14:26:24.054: E/AndroidRuntime(30993): at com.daily.morning.FirstActivity.hello(FirstActivity.java:38)
09-06 14:26:24.054: E/AndroidRuntime(30993): ... 15 more
答案 0 :(得分:2)
你应该调用Activity并将数据发送到该Activity:
Intent i2 = new Intent(FirstActivity.this, SecondActivity.class);
i2.putExtra("key","good_morning");
startActivity(i2);
在onCreate()方法的第二次活动中:
Intent n = getIntent();
String mrng = n.getStringExtras("key");
textview.setText(mrng);
你应该这样说:
i2.putExtra("key",getResource().getString(R.string.yourstring));
将 SecondActivity 类添加到AndroidManifest文件中:
答案是,您可能尚未初始化 textview ...或者找不到其ID ..
答案 1 :(得分:2)
当您的TextView
位于另一个SecondActivity
时。您需要通过setContentView()
和findViewById()
通过XML获取它,但由于FirstActivity
已经是XML Layout
拥有
View
。提取Bundle
是完全错误的。
您需要通过FirstActivity
从SecondActivity
到Key
的{{1}}传递价值,然后在那里获取。
以下是您如何实现这些目标: -
public void hello(View v)
{
Intent i1 = new Intent(firstActivity.this, SecondActivity.class);
i.putExtra("KEY",YourData); // i.putExtra("greetings", "Hello"); --> Hello is data and greeting is key to fetch from.
startActivity(i1);
finish();
}
在SecondActivity类中
您需要将从Bundle
发送到FirstActivity
的{{1}}提取到SecondActivity
,然后针对您从{{1}传递的value
获取KEY
1}}。
FirstActivity
注意:当您尝试在Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("KEY"); // String value = extras.getString("greetings"); --> which will fetch data from key greetings to value. Thus value now contains Hello.
TextView.setText(value);
}
上调用方法setText()
时,您的案例中会出现异常,TextView
中甚至不存在这种方法。这就是RunTime抛出FirstActivity
答案 2 :(得分:0)
我建议阅读 Observer Patter android
谢谢你,你会做你想做的事情:)
答案 3 :(得分:0)
尝试类似这样的代码
FirstActivity
public void hello(View v) {
Intent i1 = new Intent(firstActivity.this, SecondActivity.class);
i1.putExtra("msg","Hello");
startActivity(i1);
finish();
}
public void hru(View v) {
Intent i2 = new Intent(FirstActivity.this, SecondActivity.class);
i2.putExtra("msg","How are you ?");
startActivity(i2);
finish();
}
在onCreate()方法的第二次活动中:
textview=(TextView)findViewById(R.id.textview);
Intent n = getIntent();
String mrng = n.getStringExtras("msg");
textview.setText(mrng);
从strings.xml中获取字符串
String myString=getResources().getString("STRING_NAME");
答案 4 :(得分:0)
在第一堂课中,您必须将代码更改为:
public void hello(View v) {
Intent intent = new Intent(firstActivity.this, SecondActivity.class);
intent.putextra("data","Hello"); //Pass the value to the intent for another class
finish(); //If you want to finish your current activity
startActivity(intent);
}
public void hru(View v) {
Intent intent = new Intent(firstActivity.this, SecondActivity.class);
intent.putextra("data","How are You ?");
finish();
startActivity(intent);
}
在SecondActivity类中
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
//Get the value from the intent
Intent intent = getIntent();
String Msg = intent.getStringExtras("data"); //the parameter "data" here defines the same name as you have used while passing the argument to intent in first class
//Get your textView
TextView textView=(TextView)findViewById(R.id.textView1);
textview.setText(Msg);
....
}