【发布时间】:2016-03-11 03:57:20
【问题描述】:
所以我试图让我的按钮在我的导航抽屉片段中打开新活动,但我的 MainActivity.java 中出现了问题
我的 MainActivity.java....
package east.myapplication;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
android.support.v4.app.FragmentTransaction fragmentTransaction;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open,
R.string.drawer_close);
drawerLayout.setDrawerListener((actionBarDrawerToggle));
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home Fragment...");
navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.home_id:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home Fragment");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.id_rewards:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new RewardsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Rewards");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.id_settings:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new SettingsFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Settings Fragment");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
}
@Override
protected void onPostCreate (Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
还有我的奖励碎片...
package east.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class RewardsFragment extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_rewards);
Button button = (Button) findViewById(R.id.button);
Button anotherButton = (Button) findViewById(R.id.button2);
Button anotherButton2 = (Button) findViewById(R.id.button3);
Button anotherButton3 = (Button) findViewById(R.id.button4);
Button anotherButton4 = (Button) findViewById(R.id.button5);
Button anotherButton5 = (Button) findViewById(R.id.button6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(RewardsFragment.this, AmazonActivity.class);
RewardsFragment.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, CVSActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, SephoraActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, PlayStationActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, AMCActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, BKActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
}
}
问题是fragmentTransaction.replace(R.id.main_container, new RewardsFragment()); 它在RewardsFragment 下有一个红色下划线,我不知道如何解决它
我尝试了所有方法,这就是我目前所拥有的......
package east.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class RewardsFragment extends Fragment {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_rewards);
Button button = (Button) findViewById(R.id.button);
Button anotherButton = (Button) findViewById(R.id.button2);
Button anotherButton2 = (Button) findViewById(R.id.button3);
Button anotherButton3 = (Button) findViewById(R.id.button4);
Button anotherButton4 = (Button) findViewById(R.id.button5);
Button anotherButton5 = (Button) findViewById(R.id.button6);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(RewardsFragment.this, AmazonActivity.class);
RewardsFragment.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, CVSActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, SephoraActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, PlayStationActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, AMCActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton5.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(RewardsFragment.this, BKActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});}
}
我的 setContentView 是鲜红色的,我的 findViewByID 是红色的,最后是 (RewardsFragment.this, AmazonActivity.class);和我所有其他的 .class 一样是红色的
这是我的错误
错误:(25, 9) 错误: 找不到符号方法 setContentView(int)
Error:(26, 34) error: 找不到符号方法findViewById(int)
Error:(27, 41) error: 找不到符号方法findViewById(int)
Error:(28, 42) error: 找不到符号方法findViewById(int)
Error:(29, 42) error: 找不到符号方法findViewById(int)
Error:(30, 42) error: 找不到符号方法findViewById(int)
Error:(31, 42) error: 找不到符号方法findViewById(int)
错误:(34, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:(43, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:(53, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:(63, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:(73, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:(83, 33) 错误: 没有找到适合 Intent(RewardsFragment,Class) 的构造函数 构造函数 Intent.Intent(String,Uri) 不适用 (参数不匹配;RewardsFragment 无法转换为字符串) 构造函数 Intent.Intent(Context,Class) 不适用 (参数不匹配;无法将 RewardsFragment 转换为 Context)
错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
【问题讨论】:
-
你能发布你的日志吗
-
你在replace()中传入的对象必须是我在教程中学习过的Fragment类型的对象。请打印建议或详细登录developer.android.com/intl/es/guide/components/fragments.html