我使用了导航抽屉应用程序/项目。我只是按照我在教程中所做的那样(他们只是使用了一个不使用导航抽屉的简单项目)但是当我尝试在导航抽屉中实现它时它会崩溃。我不知道它崩溃的原因,但我认为这是碎片。
我想要发生的是:在应用程序启动时显示片段,这就是我在导航抽屉中使用'content_main.xml'的原因(我是否正确?那是显示启动内容的那个)运行应用程序?)。
我的MainActivity代码采用'onCreate'方法:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
我的searchFragment代码:
package rjj.sampleproject;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner;
public class searchFragment extends Fragment {
private searcher searcherImplementer;
public interface searcher{
public void searchForRoutesCategory(String route, String category);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.searcherImplementer =(searcher)context;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_search_category, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button flightSearchButton = (Button)getActivity().findViewById(R.id.button2);
flightSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MaterialBetterSpinner route = (MaterialBetterSpinner)getActivity().findViewById(R.id.spinner_routes);
MaterialBetterSpinner category = (MaterialBetterSpinner)getActivity().findViewById(R.id.spinner_category);
searcherImplementer.searchForRoutesCategory(route.getText().toString(),category.getText().toString());
}
});
}
}
我的content_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="rjj.sampleproject.MainActivity"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/fragment"
android:name="rjj.sampleproject.searchFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
我的activity_search_category.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/spinner_category"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Select Category"
/>
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/spinner_routes"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@id/spinner_category"
android:hint="Select Routes"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="search"
android:layout_below="@+id/spinner_routes"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/showAllRoutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:text="Show All Routes"
android:onClick="showAllRoutes"/>
</RelativeLayout>
我收到的错误:
08-19 15:04:45.281 3388-3394/? E/jdwp: Failed sending reply to debugger: Broken pipe
08-19 15:04:45.351 3388-3388/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
08-19 15:04:45.471 3388-3388/? E/EGL_emulation: tid 3388: eglSurfaceAttrib(1199): error 0x3009 (EGL_BAD_MATCH)
08-19 15:04:45.531 3388-3388/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: rjj.sampleproject, PID: 3388
java.lang.NullPointerException
at android.widget.AutoCompleteTextView.performFiltering(AutoCompleteTextView.java:858)
at com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner.onFocusChanged(MaterialBetterSpinner.java:49)
at android.view.View.handleFocusGainInternal(View.java:4617)
at android.view.View.requestFocusNoSearch(View.java:6999)
at android.view.View.requestFocus(View.java:6978)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2373)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2370)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2414)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2373)
at android.view.View.requestFocus(View.java:6945)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1828)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
MaterialBetterSpinner.java代码:
package com.weiwangcn.betterspinner.library.material;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import com.rengwuxian.materialedittext.MaterialAutoCompleteTextView;
import java.util.Calendar;
public class MaterialBetterSpinner extends MaterialAutoCompleteTextView implements AdapterView.OnItemClickListener {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
private boolean isPopup;
public MaterialBetterSpinner(Context context) {
super(context);
setOnItemClickListener(this);
}
public MaterialBetterSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
setOnItemClickListener(this);
}
public MaterialBetterSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
setOnItemClickListener(this);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering("", 0);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
setKeyListener(null);
dismissDropDown();
} else {
isPopup = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
if (isPopup) {
dismissDropDown();
isPopup = false;
} else {
requestFocus();
showDropDown();
isPopup = true;
}
}
}
}
return super.onTouchEvent(event);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
isPopup = false;
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
Drawable dropdownIcon = ContextCompat.getDrawable(getContext(), R.drawable.ic_expand_more_black_18dp);
if (dropdownIcon != null) {
right = dropdownIcon;
right.mutate().setAlpha(66);
}
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}
}
答案 0 :(得分:2)
您的MainActivity
应该实施searchFragment.searcher
并覆盖 searchForRoutesCategory
方法
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
,searchFragment.searcher {
// ^^^^^^^^^^^^^^^^^^^^^^^
否则你试图从不存在的活动中找到searcher
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.searcherImplementer =(searcher)context;
// crash , searcher not implemented by Activity ^^^^^^^^^^^^^^
}
更新:从Repository docs开始,您需要设置适配器并将其与MaterialBetterSpinner
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
BetterSpinner textView = (BetterSpinner)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
因此您需要在onCreateView
内初始化它们,它们是您的片段布局的一部分,而不是活动的一部分,因此(MaterialBetterSpinner)getActivity().
不正确
答案 1 :(得分:0)
修改强>
你去了:
这是根据我的编码修改的片段文件:
public class SearchFragment extends Fragment {
private searcher searcherImplementer;
private View mView;
private MaterialBetterSpinner route, category;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.searcherImplementer = (searcher) context;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.activity_search_category, container, false);
}
String[] list = getResources().getStringArray(R.array.month);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, list);
category = (MaterialBetterSpinner) mView.findViewById(R.id.spinner_category);
route = (MaterialBetterSpinner) mView.findViewById(R.id.spinner_routes);
route.setAdapter(adapter);
category.setAdapter(adapter);
Button flightSearchButton = (Button) mView.findViewById(R.id.button2);
flightSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searcherImplementer.searchForRoutesCategory(route.getText().toString(), category.getText().toString());
}
});
return mView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
我认为你唯一的错误就是没有为任何MaterialBetterSpinner设置适配器。希望这适合你:-)试一试。如果您仍然遇到任何问题,请告诉我。
<强> OLD 强>
尝试添加像这样的片段运行时:
您的content_main.xml将是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/llMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
然后从你的活动中添加这样的片段:
searchFragment fragment = new searchFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.add(R.id.frame,fragment, fragment.getClass().getName());
fragmentTransaction.commit();
答案 2 :(得分:0)
我终于设法显示片段并通过在searchFragment.java类中添加适配器来修复错误。
代码是:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_expandable_list_item_1, ROUTES);
MaterialBetterSpinner betterSpinner = (MaterialBetterSpinner)view.findViewById(R.id.spinner_routes);
betterSpinner.setAdapter(adapter);