将ID赋予“查看”寻呼机按钮之类的项目将无法使用

时间:2019-05-16 06:32:49

标签: android android-sqlite android-tablayout

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

这是我的主要片段

{
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"/>

1 个答案:

答案 0 :(得分:4)

您必须获得展开视图才能初始化按钮和其他视图。

{
@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;
}

初始化该片段中的所有视图。这样可以解决问题。您还可以使用ButterKnife库绑定视图。

这是链接:ButterKnife

MainFragment

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;
    }

}