teleop app android,android - Cant make a button activity on tabbed activity - Stack Overflow

该博客讲述了在Android应用中创建Tabbed Activity时遇到的问题,即无法为按钮设置点击事件监听器。作者展示了Tab3Teleop类的代码,试图为按钮设置点击事件,但遇到了错误。在MainActivity中,使用了ViewPager和TabLayout来管理各个Tab。开发者寻求帮助解决按钮点击事件的实现问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I've been tried to make a scouting app with a tabbed activity but I cant make button action,i need to make onclicklistener to a button but I get error

Error:(32, 9) error: unreachable statement"

Tab3Teleop.java:

package com.example.itai.scoutingapollo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.TextView;

/**

* Created by Itai on 10/03/2017.

*/

public class Tab3Teleop extends Fragment {

TextView CornerTV, VortexTV;

Button PlusCorner, MinusCorner, MinusVortex, PlusVortex;

int TV1 = 0;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//TODO slide to position

View rootView = inflater.inflate(R.layout.tab3teleop, container, false);

return rootView;

CornerTV = (TextView) rootView.findViewById(R.id.CornerTV);

VortexTV = (TextView) rootView.findViewById(R.id.VortexTV);

PlusVortex = (Button) rootView.findViewById(R.id.PlusVortex);

PlusCorner = (Button) rootView.findViewById(R.id.PlusCorner);

MinusCorner = (Button) rootView.findViewById(R.id.MinusCorner);

MinusVortex = (Button) rootView.findViewById(R.id.MinusVortex);

PlusCorner.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

CornerTV.setText(TV1 + 1);

}

});

return rootView;

}

}

And this the MainActivity:

package com.example.itai.scoutingapollo;

import android.support.design.widget.TabLayout;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

/**

* The {@link android.support.v4.view.PagerAdapter} that will provide

* fragments for each of the sections. We use a

* {@link FragmentPagerAdapter} derivative, which will keep every

* loaded fragment in memory. If this becomes too memory intensive, it

* may be best to switch to a

* {@link android.support.v4.app.FragmentStatePagerAdapter}.

*/

private SectionsPagerAdapter mSectionsPagerAdapter;

/**

* The {@link ViewPager} that will host the section contents.

*/

private ViewPager mViewPager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

// Create the adapter that will return a fragment for each of the three

// primary sections of the activity.

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.

mViewPager = (ViewPager) findViewById(R.id.container);

mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

tabLayout.setupWithViewPager(mViewPager);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

fab.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Snackbar.make(view, "Scouting Saved", Snackbar.LENGTH_LONG)

.setAction("Action", null).show();

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

/**

* A {@link FragmentPagerAdapter} that returns a fragment corresponding to

* one of the sections/tabs/pages.

*/

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {

super(fm);

}

@Override

public Fragment getItem(int position) {

switch (position) {

case 0:

Tab1Setup tab1 = new Tab1Setup();

return tab1;

case 1:

Tab2Auto tab2 = new Tab2Auto();

return tab2;

case 2:

Tab3Teleop tab3 = new Tab3Teleop();

return tab3;

case 3:

Tab4End tab4 = new Tab4End();

return tab4;

default:

return null;

}

}

@Override

public int getCount() {

//TODO set slides number

return 4;

}

@Override

public CharSequence getPageTitle(int position) {

switch (position) {

//TODO set slides names

case 0:

return "Setup";

case 1:

return "Auto";

case 2:

return "Teleop";

case 3:

return "End";

}

return null;

}

}

}

Hope you can help... thanks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值