我是编程新手,我在互联网上找不到任何理想的解决方案,因此我希望你能帮助我
我想编写一个应用程序,它有4个标签,每个标签都有一个ListView。如果我创建的应用程序只有4个选项卡或只有ListView,它工作得很好。但是,当谈到将这两个部分结合起来时,它就不起作用了。它在启动应用程序时开始崩溃
请看看我的代码。我只会显示一个标签java和xml代码,因为它总是一样的。
感谢您抽出宝贵时间。
MainActivity.java
dtres %>%
group_by(t) %>%
summarise(mN = mean(new))
activity_main.xml中
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
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);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Tab1.java
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.matrix.koks.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_3" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_4" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
fragment_1.xml
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.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Tab1 extends Fragment {
EditText EditText;
Button AddBtn;
Button SubBtn;
Button ResultBtn;
Button ResetButton;
ListView ListView;
ArrayList<String> ArrayListString;
ArrayAdapter<String> Adapter;
ArrayList<Integer> ArrayListInteger;
int Value;
int Count;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
EditText = (EditText) getView().findViewById(R.id.EnterValueEditText1);
AddBtn = (Button) getView().findViewById(R.id.AddDataButton1);
SubBtn = (Button) getView().findViewById(R.id.SubDataButton1);
ResultBtn = (Button) getView().findViewById(R.id.ResultButton1);
ResetButton = (Button) getView().findViewById(R.id.ResetButton1);
ListView = (ListView) getView().findViewById(R.id.ListView1);
ArrayListString = new ArrayList<String>();
Adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, ArrayListString);
ArrayListInteger = new ArrayList<Integer>();
ListView.setAdapter(Adapter);
AddBtnClick();
SubBtnClick();
ResultButtonClick();
ResetButtonClick();
return rootView;
}
public void AddBtnClick(){
AddBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Number = EditText.getText().toString();
ArrayListString.add(Number);
Adapter.notifyDataSetChanged();
Value = Integer.parseInt(EditText.getText().toString());
Count += Value;
EditText.getText().clear();
}
});
}
public void SubBtnClick(){
SubBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Number = EditText.getText().toString();
ArrayListString.add(Number);
Adapter.notifyDataSetChanged();
Value = Integer.parseInt(EditText.getText().toString());
Count -= Value;
EditText.getText().clear();
}
});
}
public void ResultButtonClick(){
ResultBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayListString.clear();
Adapter.notifyDataSetChanged();
String Final = String.valueOf(Count);
ArrayListString.add(Final);
Adapter.notifyDataSetChanged();
}
});
}
public void ResetButtonClick(){
ResetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayListString.clear();
Adapter.notifyDataSetChanged();
Count = 0;
}
});
}
}
logcat的
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/EnterValueEditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/EditText"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/AddDataButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/AddData"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.275"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/EnterValueEditText1" />
<Button
android:id="@+id/SubDataButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/SubData"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.725"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/EnterValueEditText1" />
<Button
android:id="@+id/ResultButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="@string/Result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.275"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/AddDataButton1" />
<Button
android:id="@+id/ResetButton1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="@string/Reset"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.725"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/SubDataButton1" />
<ListView
android:id="@+id/ListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/ResultButton1" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
使用FloatingActionButton
代替
示例强>
rootView