【问题标题】:Button won't open new activity按钮不会打开新活动
【发布时间】: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' 执行失败。

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

【问题讨论】:

标签: java android


【解决方案1】:
public class RewardsFragment extends Activity

您的“片段”实际上是一个 Activity。

更改此项以扩展 Fragment 并添加一个空白的公共构造函数,即

public class RewardsFragment extends Fragment {

    public RewardsFragment(){
    //Default blank constructor 
    }

    //The rest of your code here

}

另外,您应该为@Bind 和@Onclick 使用really consider using Butterknife,而不是在代码中使用这么多的 findViewByIds 和 click 侦听器。这将有助于开始清理它。

【讨论】:

  • 之后,RewardsFragment{ 出现错误提示 Identifier expected?
  • 对不起,我打错了代码,应该是 public RewardsFragment(){ }。我会更新答案。
  • @ErickBachhuber 在最后一个 } 之后,还有另一个错误提示“类”或“接口”预期
  • 你可能只是有一个太多的结尾花括号。删除未使用的}
  • 你能给我看一个黄油刀绑定的例子吗?
【解决方案2】:

你必须

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;
    }

}

}

【讨论】:

  • 导入android.support.v4.app.Fragment;
  • 为什么要切换我的 Home Fragment?
  • 这是一个例子!。您可以将 HomeFragment 替换为 YourFragment()
  • 你的问题必须是“YourFragment extend Fragment”并且R.id.main_container是FrameLayout的“id”
  • 我发布了关于代码有什么问题的更新,每次我尝试修复它时,都会出现另一个错误
猜你喜欢
  • 2019-03-07
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多