【发布时间】:2018-07-21 14:36:15
【问题描述】:
AppCompactActivity 类链接到 3 个片段,您可以在这些片段上移动底部菜单。所有代码都在 AppCompactActivity 中。该应用程序在设置 seekbar 和 textview 时将我标记为错误 [尝试在空对象引用上调用虚拟方法 'void android.widget.SeekBar.setMax(int)']。 我不想移动各个Fragments的java文件中的代码,是否可以同样解决问题?
主要活动
public class OpSearchPlaceActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Button get_place, bSave;
EditText etPosition;
SeekBar sbDistance;
TextView tvProgress,tvLat, tvLon;
int progress = 2;
int maxProgress = 80;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
public static final String LoginPREF = "login";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_op_search_place);
get_place = (Button) findViewById(R.id.getPlace);
bSave = (Button) findViewById(R.id.savePlace);
etPosition = (EditText) findViewById(R.id.position);
sbDistance = (SeekBar) findViewById(R.id.sbDistance);
tvProgress = (TextView) findViewById(R.id.tvProgress);
tvLat = (TextView) findViewById(R.id.tvLat);
tvLon = (TextView) findViewById(R.id.tvLon);
sbDistance.setMax(maxProgress);
sbDistance.setProgress(progress);
tvProgress.setText("" + progress);
sbDistance.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
progress = i;
tvProgress.setText("" + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
布局主活动
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/dlMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ale.butastupa.OpSearchPlaceActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_nav"></FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#c1c1c1"
app:menu="@menu/bottom_menumap">
</android.support.design.widget.BottomNavigationView>
<android.support.design.widget.NavigationView
android:id="@+id/nvMenu"
app:headerLayout="@layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ffffff"
app:itemTextColor="@color/colorPrimaryDark"
app:itemIconTint="@color/colorPrimaryDark"
app:menu="@menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
片段 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<EditText
android:id="@+id/position"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="posizione attuale"
android:layout_weight="2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SELEZIONA PUNTO DIVERSO"
android:id="@+id/getPlace"
android:layout_weight="3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:weightSum="5">
<SeekBar
android:id="@+id/sbDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tvProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:textSize="24dp"
android:layout_weight="4"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</LinearLayout>
<TextView
android:id="@+id/tvLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/tvLon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CERCA"
android:id="@+id/savePlace"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
Java 片段 1
public class FilterOpSearchFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_opsearch_filter, container, false);
return rootView;
}
}
【问题讨论】:
-
您有一个空对象引用。我认为您的 SeekBar 没有实例化。您是否从您的视图中获得了 SeekBar?在您的代码中应该看起来像这样:
SeekBar simpleSeekBar = (SeekBar) findViewById(R.id.simpleSeekBar); -
项目已经初始化 public class OpSearchPlaceActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { SeekBar sbDistance; int 进度 = 2; int maxProgress = 80; … @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_op_search_place); … sbDistance = (SeekBar) findViewById(R.id.sbDistance); sbDistance.setMax(maxProgress); sbDistance.setProgress(progress);
-
您是否在代码的任何其他部分使用搜索栏?当您使用 seekbar 作为实例变量时,我的猜测是您在 onCreate 方法中实例化它之前更改了它的一些值。如果您可以在有关 SeekBar 用法的问题中包含一些代码,那就太好了。
-
已编辑,我添加了代码
-
啊,我明白了,我想你可能需要在你的片段类中添加你的实例,你不能直接从像这样的另一个布局中获取它们。为什么不跳过片段并将所有组件直接添加到 MainActivity 布局中。我认为没有一种简单的方法可以将所有组件从片段获取到 MainActivity。如果您要使用 MainActivity 中的所有组件,我只是看不出有片段的意义。抱歉直到我看到代码才明白你的问题。
标签: java android android-fragments appcompatactivity