0

可能是一个简单的错误,但 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();
4

3 回答 3

1

试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.detailedscreen);  //set the layout here

final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname); //then get id of any view
    Intent in = getIntent();
    String name = in.getStringExtra("name");//gets name here and make sure in first activity you are not passing empty string 

    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
    changetitle.setText(name);
}

编辑

如果在第一个活动中您传递了这样的字符串:

intent.putExtra("name", selected);

然后像这样在第二个活动中得到它

name=in.getStringExtra("name");

您的 toast 消息是正确的,您可以通过显示如下简单文本来检查这一点:

  Toast.makeText(this,"Toast works fine " , 3000).show();

请参阅此示例如何将字符串传递给另一个活动。

于 2013-04-04T13:12:54.983 回答
0

重构你的 onCreate() 方法

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detailedscreen);

    //your code
}
于 2013-04-04T13:13:58.093 回答
0
  • 首先,始终确保在调用 findViewById() 之前调用 setContentView()

您没有发布将字符串发送到此活动的代码,但我假设

-1。你发送的值是空的,-2。您不发送值 -3。StringExtra 名称在此和之前的活动中不同,即“名称”

于 2013-04-04T13:22:29.690 回答