gpt4 book ai didi

android - 将 Id 提供给 View pager 像按钮这样的项目是行不通的

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:32 24 4
gpt4 key购买 nike

我想实现包含两个数据页面的 View 分页器,都包含搜索栏、一个按钮和一个 ListView ,这两个 View 分页器都需要从 SQLite 获取数据...当我尝试为 Java 文件中的按钮和列表提供 ID 时,应用程序崩溃了,并说这在这里是不可能的。

这是我的主要 fragment

{
ListView listViewclients;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Dynamic ERPMini");
TabLayout tabLayout = findViewById(R.id.tab_layout);

//listViewclients=findViewById(R.id.listclient);

tabLayout.addTab(tabLayout.newTab().setText("Clients"));
tabLayout.addTab(tabLayout.newTab().setText("Products"));
tabLayout.addTab(tabLayout.newTab().setText("Invoices"));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagersAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}
}

这里是clients.java

{
ListView listView;
Button btnAll;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.clients, container, false);
}

}

这是第二张幻灯片

{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.products, container, false);
}
}

这里是产品和客户端的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">

<SearchView
android:id="@+id/searchclient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:layout_alignParentRight="true">

</SearchView>

<Button
android:id="@+id/searchall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchclient"
android:layout_marginTop="15dp"
android:background="@color/colorPrimaryDark"
android:padding="10dp"
android:text="Search All"
android:textColor="@color/colorPrimary" />

<android.support.v7.widget.RecyclerView
android:layout_below="@+id/searchall"
android:id="@+id/recyclerViewclients"
android:layout_width="match_parent"
android:layout_height="match_parent" />

这是产品xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical">

<SearchView
android:id="@+id/searchproduct"
android:layout_width="match_parent"
android:outlineSpotShadowColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:layout_alignParentRight="true">
</SearchView>

<Button
android:id="@+id/searchallproducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchproduct"
android:layout_marginTop="15dp"
android:background="@color/colorPrimary"
android:padding="10dp"
android:text="Search All"
android:textColor="@color/colorPrimary" />

<android.support.v7.widget.RecyclerView
android:layout_below="@+id/searchallproducts"
android:id="@+id/recyclerViewUsers"
android:layout_width="match_parent"
android:layout_height="match_parent" />

错误在这里结束Error Location Image

这是 Logcat

ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tablewithlist/com.example.tablewithlist.MainFragment}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tablewithlist.MainFragment.onCreate(MainFragment.java:29)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

这是将 id 传递给 client.java 类后的图像 Image Of Passing ID to Client.java

这是我的主要 fragment.xml 文件

<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"
android:background="@color/colorPrimary"
tools:context="com.example.tablewithlist.MainFragment">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorHeaderBackground"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="@color/colorHeaderBackground"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>

最佳答案

您必须获取膨胀 View 才能初始化按钮和其他 View 。

{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.products, container, false);
buttonAll= view.findViewById(R.id.button);

buttonAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// do something
}
});

return view;
}

像这样初始化 fragment 中的所有 View 。这将解决问题。您还可以使用 ButterKnife 库来绑定(bind) View 。

这是链接:ButterKnife

主 fragment

public class MainFragment extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Dynamic ERPMini");

TabLayout tabLayout = findViewById(R.id.tab_layout);


tabLayout.addTab(tabLayout.newTab().setText("Clients"));
tabLayout.addTab(tabLayout.newTab().setText("Products"));
tabLayout.addTab(tabLayout.newTab().setText("Invoices"));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagersAdapter(getSupportFragmentManager(), tabLayout.getTabCount());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}
}

Clients.Java

public class Clients extends Fragment {
ListView listView;
Button btnAll;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.clients, container, false);
btnAll=view.findViewById(R.id.searchall);
listView=view.findViewById(R.id.Viewclients);

btnAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Usman", Toast.LENGTH_SHORT).show();
}
});

return view;
}

}

关于android - 将 Id 提供给 View pager 像按钮这样的项目是行不通的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56162184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com