基本上我正在尝试为fragment_home_screen活动设置一些按钮监听器。我继续运行的唯一问题是在获取实例化按钮的正确视图时。你看,视图是在一个静态类中,所以当我尝试设置按钮监听器时,任何内部调用都必须是静态对象。
到目前为止我的应用程序有一个滑动抽屉和一个起始片段。
我想从侦听器中调用非静态方法。有什么方法可以解决这个问题,还是可能的解决方案?
[向下滚动到onCreateView方法的中间位置以查看问题]
public class HomeScreen extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private BluetoothSocket mmSocket;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mmDevice;
private UUID MY_UUID;
private final String NAME = "Acceptor";
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
startUpBluetooth();
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.home_screen, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
/
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false);
try {
Button acceptButton = (Button) rootView.findViewById(R.id.acceptButton);
Button connectedButton = (Button) rootView.findViewById(R.id.connectedButton);
Button connectButton = (Button) rootView.findViewById(R.id.connectButton);
acceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ***problem is here***
AcceptThread a = new AcceptThread();
a.run();
}
});
connectedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectedThread cc = new ConnectedThread();
cc.run();
}
});
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectThread con = new ConnectThread();
con.run();
}
});
}
catch(Exception e1 ){
errorToast("We have a button problem: "+e1.toString());
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((HomeScreen) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
public class ConnectThread extends Thread {
public ConnectThread() {
...
}
public void run() {
...
}
public void cancel() {
...
}
}
public class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread() {
...
}
public void run() {
...
}
public void write(byte[] bytes) {
...
}
public void cancel() {
...
}
}
//this class is in the same file
public class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
...
}
public void run() {
...
}
public void cancel() {
....
}
}
}
这是XML:
Fragment_home_screen.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".HomeScreen$PlaceholderFragment">
<TextView android:id="@+id/largeText" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connected Thread"
android:id="@+id/acceptButton"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/connectButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect Thread"
android:id="@+id/connectedButton"
android:layout_below="@+id/acceptButton"
android:layout_alignEnd="@+id/acceptButton"
android:layout_toEndOf="@+id/largeText"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept Thread"
android:id="@+id/connectButton"
android:layout_above="@+id/acceptButton"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/largeText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_alignEnd="@+id/connectButton"
android:layout_above="@+id/connectButton"
android:layout_toEndOf="@+id/largeText"
android:layout_below="@+id/largeText" />
Activity_home_screen.xml
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".HomeScreen">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.gregorybolet.scoutingapp.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
答案 0 :(得分:0)
我想从侦听器中调用非静态方法。是 有什么方法可以解决这个问题,还是可能的解决方案?
要访问AcceptThread
和ConnectedThread
类:
选项1。创建两个类static
,因为我们只能访问静态范围中的静态方法和类
选项2。从static
班级
PlaceholderFragment
同时更改newInstance
方法:
public PlaceholderFragment newInstance(int sectionNumber) {
return fragment;
}
在FragmentManager中添加时使用new获取PlaceholderFragment类的对象:
fragmentManager.beginTransaction()
.replace(R.id.container,new PlaceholderFragment.newInstance(
position + 1))
.commit();
第二个选项很容易,而不是让所有类都静态只需从static
类中删除PlaceholderFragment
答案 1 :(得分:0)
我不确定静态类是否是最好的方法。
一些使其更容易的建议:
PlaceholderFragment()
; setArguments(Bundle extras)
方法传递位置。基本上,做newInstance
代码在外面发生的任何事情。
或... 强>
为您的片段创建一个新的构造函数:
public PlaceholderFragment(int sectionNumber) {
this();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
setArguments(args);
}
绝不是@Override
默认构造函数。 Android使用它在需要时重新创建片段。
创建这个新的构造函数可以让你这样做:
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.container,
new PlaceholderFragment(position + 1)).commit();
}