我正在嘗試製作android應用程序。它有兩個活動:MainActivity和Activity_Service。在MainActivity中,有一個按鈕「service」用於傳遞給Activity_Service。我想從MainActivity發送一個字符串到Activity_Service,然後在textview中顯示它。我想向你展示代碼。在MainActivity 創建意圖:無法更改Android上第二個活動的TextView中的文本
service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String message = "bla bla bla";
Intent intent = new Intent(MainActivity.this, ActivityService.class);
intent.putExtra("message", message);
startActivity(intent);
}
});
而在Activity_Service onCreate()方法:
public class ActivityService extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
System.out.println(message);
//TextView txtView = new TextView(this);
TextView txtView = (TextView)findViewById(R.id.name);
txtView.setText(message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activity,
container, false);
return rootView;
}
}
}
的XML文件,同時創造新的活動由Eclipse中創建(Activity_Service) : activity_service.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alljoynandroid.ActivityService"
tools:ignore="MergeRootFrame" />
Fragment_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:orientation="vertical"
tools:context="com.example.alljoynandroid.ActivityService$PlaceholderFragment" >
<TextView
android:id = "@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在的onCreate()Activity_Service的方法,如果我用TextView txtView = new TextView(this);
,它的工作原理:它可以從MainActivity傳遞到Activity_Service,但我們不能的setText。另一方面,如果我使用TextView txtView = (TextView)findViewById(R.id.name);
,它不會全部工作。你能幫我解決嗎?我正在嘗試2天,現在我不知道。
哦,它現在。非常感謝你。您能否向我解釋爲什麼我們必須刪除:if(savedInstanceState == null){getResourceFragmentManager()。beginTransaction() .add(R.id.container,new PlaceholderFragment())。commit(); }。對不起,這個問題BC我是一個Android初學者。 – 2014-10-03 12:15:40
@VUONGTam您的textView來自'Fragment_activity'佈局,並且您的'activity_service'佈局中沒有找到textView。 – 2014-10-03 12:19:08
非常感謝您的解釋。 :) – 2014-10-03 12:26:58