可能是一个简单的错误,但 logcat 让我感到困惑。我刚刚将一个变量“名称”从一个列表视图传递给了这个活动,并希望将标题 textview 更改为该变量,使用...“detailedsocietyname”被用作 XML 文件中的 textview ID
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
当那不起作用时,我尝试使用 toast 来测试它,而不是使用
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
The issue is that when the listview choice is chosen the app shuts down.... if all the code above is commented out, it works but the default screen title of 'example' is shown.
提前致谢,
完整代码
package com.apolloapps.ntusoc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class DetailedScreen extends Activity {
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
//getActionBar().setDisplayHomeAsUpEnabled(true);
Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
changetitle.setText(name);
this.setContentView(R.layout.detailedscreen);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detailedscreen, menu);
return true;
}
}
编辑:第一个活动意图代码
final String selected = (String) parent.getSelectedItem();
Intent i = new Intent(getApplicationContext(), DetailedScreen.class);
i.putExtra("name", selected);
startActivity (i);
解决方案:使用
final String name = items[position];
代替
final String selected = (String) parent.getSelectedItem();