gpt4 book ai didi

Android Studio - 预期的表达

转载 作者:行者123 更新时间:2023-11-30 01:58:10 25 4
gpt4 key购买 nike

我正在创建一个天气应用程序,我收到一条消息,提示需要一个表达式。

这是我的主要 Activity :

package com.haziqhussain.hazweather;

import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class WeatherActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_weather);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}



}

@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);*/
if(item.getItemId() == R.id.change_city){
showInputDialog();
}
return false;

}


private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}

public void changeCity(String city){
WeatherFragment wf = (----->WeatherFragment<------).getSupportFragmentManager()
.findFragmentById(R.id.container);
wf.changeCity(city);
new CityPreference(this).setCity(city);
}

}

错误出现在调用天气 fragment 的 Public Void ChangeCity 中。这是一个不断发生的错误,我不确定发生了什么。

最佳答案

使用这个:

public void changeCity(String city){
WeatherFragment wf = (WeatherFragment) getSupportFragmentManager()
.findFragmentById(R.id.container); // removed '.'
wf.changeCity(city);
(new CityPreference(this)).setCity(city);

关于Android Studio - 预期的表达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com