0

我想顯示一個由BottonNavigationView使用的片段內的棘手的列表視圖(除其他事項外)。我所做的是,創建一個包含BottomNavigationView的主要活動,以及包含其他視圖的片段。 listView應該連接到Firebase數據庫。對於文件區域的代碼如下:添加一個棘手的列表視圖片段爲BottomNavigationView

JobList.java

package com.example.sherwin.todo; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

import com.google.firebase.database.ChildEventListener; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 

public class JobList extends Fragment { 

public JobList() { 
} 
public static JobList newInstance() { 
    JobList fragment = new JobList(); 

    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_job_list, container, false); 

    // Create a new Adapter 
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), 
      android.R.layout.simple_list_item_1, android.R.id.text1); 

    // Get ListView object from xml 
    final ListView listView = (ListView)rootView.findViewById(R.id.listView); 

    // Assign adapter to ListView 
    listView.setAdapter(adapter); 

    // Connect to the Firebase database 
    FirebaseDatabase database = FirebaseDatabase.getInstance(); 

    // Get a reference to the todoItems child items it the database 
    final DatabaseReference myRef = database.getReference("JOBS"); 

    // Assign a listener to detect changes to the child items 
    // of the database reference. 
    myRef.addChildEventListener(new ChildEventListener() { 

     // This function is called once for each child that exists 
     // when the listener is added. Then it is called 
     // each time a new child is added. 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 

      String value = dataSnapshot.getKey(); 
      adapter.add(value); 
     } 

     // This function is called each time a child item is removed. 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 
      String value = dataSnapshot.getKey(); 
      adapter.remove(value); 
     } 

     // The following functions are also required in ChildEventListener implementations. 
     public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) { 
     } 

     public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) { 
     } 

     @Override 
     public void onCancelled(DatabaseError error) { 
      // Failed to read value 
      Log.w("TAG:", "Failed to read value.", error.toException()); 
     } 
    }); 

    ///to send to next page 
    Button nextPage = (Button) rootView.findViewById(R.id.addNewJob); 

    // Capture button clicks 
    nextPage.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      // Start NewActivity.class 
      Intent myIntent = new Intent(getContext(), 
        JobForm.class); 
      startActivity(myIntent); 
     } 
    }); 

    return rootView; 
} 

} 

MainActivity.java

package com.example.name.todo; 

import android.support.annotation.NonNull; 
import android.support.design.widget.BottomNavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 


private TextView mTextMessage; 

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     Fragment selectedFragment = null; 
     switch (item.getItemId()) { 
      case R.id.navigation_home: 
       selectedFragment = HomePage.newInstance(); 
       break; 
      case R.id.navigation_job: 
       selectedFragment = JobList.newInstance(); 
       break; 
      case R.id.navigation_machine: 
       selectedFragment = MachinePage.newInstance(); 
       break; 
      case R.id.navigation_mail: 
       selectedFragment = MessagePage.newInstance(); 
       break; 
      case R.id.navigation_resources: 
       selectedFragment = ResourcePage.newInstance(); 
       break; 
     } 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.content, selectedFragment); 
     transaction.commit(); 
     return true; 
    } 

}; 

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

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 
} 

} 

fragment_job_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical"> 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="2.8"> 

<ListView 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_above="@+id/addNewJob" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true"></ListView> 

<Button 
    android:id="@+id/addNewJob" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:text="@string/add_job" /> 

</RelativeLayout> 

</LinearLayout> 

的logcat中吐出來的是以下幾點:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.name.todo/com.example.name.todo.JobList}: java.lang.ClassCastException: com.example.name.todo.JobList cannot be cast to android.app.Activity 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                    Caused by: java.lang.ClassCastException: com.example.name.todo.JobList cannot be cast to android.app.Activity 
                     at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
                     at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:154)  
                     at android.app.ActivityThread.main(ActivityThread.java:6119)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

我不知道該從哪裏做什麼幫助,將不勝感激。

謝謝

+0

它是一個ClassCastException。您是否驗證AndroidManifest.xml文件中的活動條目? – JohnC

+0

謝謝@JohnC我確實看過清單,看到它將碎片聲明爲活動(導致錯誤)。我更新了它們,現在完美地工作。謝謝。 –

+0

不客氣。我很高興它有幫助! – JohnC

回答

0

@JohnC說什麼。更新清單以反映JobList.java不是活動

相關問題