我编写了下面的代码来引用名为 Forecast Fragment 的类,并且应用程序运行。但在第 33 行,我收到错误“无法解析符号‘android’”
package com.example.sunshinepre_beta;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
int commit = getSupportFragmentManager().beginTransaction()
.add(R.id.container, new com.example.android.sunshinepre_beta.app.ForecastFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
}
最佳答案
将 new com.example.android.sunshinepre_beta.app.ForecastFragment()
替换为 new ForecastFragment()
还将 ForecastFragment.java 移动到 MainActivity 所在的同一文件夹中。
关于java - Android Studio,调用不同文件中的不同类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26695296/