嗨,大家好,我是Android的初学者,但是我想做的就是在我按地图上的infoWindowAdapter时开始一个新的活动。该代码似乎很简单,但是它不起作用,我不知道为什么。你能帮助我吗?我在清单中声明了我的新活动(MoreInfoActivity),我想从主活动中调用它。在下面,您可以找到我的所有“ MoreInfoActivity”源代码,它来自MainActivity的调用以及清单中的声明。非常感谢
当我按下InfoWindowAdapter时从MainActivity调用:[更新]
final Activity thiz = this;
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
/*Nuova chiamata di un activity (scelta perché più pratica e la richiamiamo anche dalla listview)*/
/*************************************************************************************************/
String nome_luogo = marker.getTitle();
Intent piu_info = new Intent(thiz, MoreInfoActivity.class);
piu_info.putExtra("nome", nome_luogo);
startActivity(piu_info);
/*************************************************************************************************/
}
});
MoreInfoActivity:[更新]
public class MoreInfoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moreinfo);
Bundle extras = getIntent().getExtras();
if (extras != null){
String value = extras.getString("nome");
prendi_informazioni(value);
}
}
public void prendi_informazioni(String nome_luogo){
View layout = getWindow().getDecorView().findViewById(R.id.immagine_luogo);
//String nome_luogo = marker.getTitle();
BuildInfoMatrix printinfo = new BuildInfoMatrix();
printinfo.Fill_Matrix();
printinfo.Read_Matrix(nome_luogo, layout);
}
}
表现:
<!-- More informations Activity -->
<activity
android:name="com.example.findmyclients.MoreInfoActivity"
android:screenOrientation="portrait">
</activity>
Logcat:http://pastebin.com/pKuW4LCk
最佳答案
我不确定您的语法是否正确。我要询问的部分是MainActivity.this
。我不确定这意味着什么,但可能是该类的一个实例。相反,请在clickListener之外创建一个Activity变量,并从clickListener中进行引用。例:
final Activity thiz = this; /* Needs to be final so you can reference it in inner class */
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
View v2 = getLayoutInflater().inflate(R.layout.activity_moreinfo, null);
MoreInfoActivity prova = new MoreInfoActivity();
prova.prendi_informazioni(marker);
Intent piu_info = new Intent(thiz, MoreInfoActivity.class);
startActivity(piu_info);
}
});
附带说明,您永远不要自己实例化Activity的实例。如果您需要向活动传递额外的参数,请通过添加额外功能将其附加到意图。看这里:
http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String,double [])
例如:
String nome_luogo = marker.getTitle();
piu_info.putExtra("marker", nome_luogo );
并在onCreate的MoreInfoActivity中:
String marker = getIntent().getExtras().getString("marker");
doSomething(marker);
现在的操作方式,即使没有得到空指针异常,MoreInfoActivity也不会看到您的参数