我已尝试使用标签式活动制作一个侦察应用但我无法进行按钮操作,我需要将onclicklistener设为按钮但我收到错误
错误:(32,9)错误:无法访问的语句"
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;
}
}
这是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;
}
}
}
希望你能帮忙......谢谢。
答案 0 :(得分:1)
只需删除不必要的return语句:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//TODO slide to position
View rootView = inflater.inflate(R.layout.tab3teleop, container, false);
//Remove this:
//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;
}