当前位置: 首页 > 知识库问答 >
问题:

按钮不会打开新的activity

丁学
2023-03-14

所以我试图让我的按钮在我的导航抽屉碎片中打开新的活动,但是我的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);是红色的和我所有的。班上的

这是我的虫子

错误:(25,9)错误:找不到符号方法setContentView(int)

错误:(26,34)错误:找不到符号方法findViewById(int)

错误:(27,41)错误:找不到符号方法findViewById(int)

错误:(28,42)错误:找不到符号方法findViewById(int)

错误:(29,42)错误:找不到符号方法findViewById(int)

错误:(30,42)错误:找不到符号方法findViewById(int)

错误:(31,42)错误:找不到符号方法findViewById(int)

错误:(34,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:(43,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:(53,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:(63,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:(73,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:(83,33)错误:找不到适合Intent(RewardsFragment,Class)构造函数意图的构造函数。Intent(String,Uri)不适用(参数不匹配;RewardsFragment无法转换为String)构造函数意图。Intent(Context,Class)不适用(参数不匹配;RewardsFragment无法转换为Context)

错误:任务“:app:CompileDebugJavaWithJavac”执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。

共有2个答案

梁丘安晏
2023-03-14
public class RewardsFragment extends Activity

你的“碎片”实际上是一个activity。

将此更改为Extended Fragment并添加一个空白的公共构造函数,即。

public class RewardsFragment extends Fragment {

    public RewardsFragment(){
    //Default blank constructor 
    }

    //The rest of your code here

}

另外,您应该考虑在代码中为@bind和@onclick使用Butterknife,而不是使用那么多的findViewByIds和click侦听器。这将有助于开始清理它。

元叶秋
2023-03-14

你必须

RewardsFragment extend Fragment 

import android.support.v4.app.Fragment;

而且

anotherButton3.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        // creating the intent
        Intent intent = new Intent(getActivity,    PlayStationActivity.class);
        // starting activity with the created intent
        getActivity.startActivity(intent);
    }
});

替换

 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;

 FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.content_main, new HomeFragment())
            .commit();

这是示例扩展片段

public class SettingFragment extends Fragment implements View.OnClickListener {
private TextView mTextTest;
 private TextView mTextTest1;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_setting, container, false);
    addControl(view);

    return view;
}



private void addControl(View view) {
    mTextTest = (TextView) view.findViewById(R.id.text_test);
mTextTest1 = (TextView) view.findViewById(R.id.text_test_1);
    mTextTest.setOnClickListener(this);
  mTextTest1.setOnClickListener(this);

}


@Override
public void onClick(View v) {

    FragmentManager manager = getActivity().getSupportFragmentManager();
    switch (v.getId()) {
        case R.id.text_test:
            manager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.content_main, new YourFragment())
                    .commit();
            break;
   case R.id.text_test_1:
  startActivity(new Intent(getActivity(), YourActivity.class))
            break;
        default:
            break;
    }

}

}

 类似资料:
  • 我是全新的,我唯一的编程经验是PLC; s和HMI的,所以我很抱歉不能弄清楚什么可能是一个简单的任务。但是我找不到任何CURRENT视频或工作示例,说明如何简单地将图像放置在主活动上,并在按下后打开另一个活动。我正在使用最新版本的Android Studio11.0,并试图学习静态编程语言。我已经附上了我到目前为止的项目代码,正如你所看到的,我刚刚开始这个项目。提前感谢您的任何帮助。 Java文件

  • 我合并了一个水平幻灯片导航组件(这需要使类扩展片段)。滑动部分工作正常。这里我有相应的<code>onClick() 到目前为止,我的代码还不多,所以我不会用相关的布局部分来混淆我的问题。任何帮助都是非常感谢的。 片段#1的Java代码 public class TasksFragment扩展Fragment { @ Override public View oncreate View(layou

  • 按钮处理程序: 类ButtonHandler实现ActionListener{public void actionPerformed(ActionEvent e){

  • 问题内容: 你们可以帮我将以下代码翻译成Swift吗? (或者我是否必须使用此链接: _itms://itunes.apple.com/app/id839686104_ ?) 提前致谢! 问题答案: 这里。但我强烈建议您学习Swift的基础知识!

  • 我正在编写一个包含许多按钮的程序,当我单击其中一个按钮时,我希望它打开一个包含一些txt文件信息的JTextArea。我试图将JTextArea添加到按钮中,但它没有打开任何东西。我是Java新手,所以我不知道这是否是正确的方法。 我在GUI中定义了按钮和JTextArea 这是我在public void actionPerformed(ActionEvent e)中编写的代码