1

我有两个活动第一个有两个按钮第二个有一个文本视图

我需要的是当我单击第一个按钮时,第二个活动打开并且 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();
}

}

当我单击第一个按钮或第二个按钮时,应用程序强制关闭

这是日志猫

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

5 回答 5

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 文件中:

Ans 是的,可能是你没有初始化你的textview ...或者没有找到它的 id ..

于 2013-09-06T11:36:38.390 回答
2

正如您TextView在另一个SecondActivity.Firts 中一样,您需要通过 XML 通过setContentView()and获取它findViewById(),但FirstActivity它已经是自己的 .Firts 了XML Layout。取货View是完全错误的。

Bundle您需要通过在from FirstActivityto SecondActivitywith some中传递值并在Key那里获取它来实现您的目标。

以下是如何实现这些目标的简要说明:-

 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发送的内容FirstActivitySecondActivityvalue针对KEY您从您的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()于. 这就是 RunTime 抛出的原因TextViewFirstActivityIllegalStateException

于 2013-09-06T11:38:31.623 回答
0

尝试这样的代码

第一活动

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");
于 2013-09-06T11:44:09.783 回答
0

我推荐阅读 Observer Patter android

谢谢你,你会做你想做的事:)

于 2013-09-06T11:36:56.367 回答
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);
....

}
于 2013-09-06T12:49:40.230 回答