0

Ich versuche, eine widerspenstige Listenansicht (unter anderem) innerhalb eines Fragments, das von der BottonNavigationView verwendet wird, anzuzeigen. Was ich getan habe, ist, eine Hauptaktivität zu erstellen, die die BottomNavigationView enthält, und Fragmente, die andere Ansichten enthalten. Die ListView sollte eine Verbindung zu einer Firebase-Datenbank herstellen. Der Code für den Bereich Dateien wie folgt:Hinzufügen eines hartnäckigen ListView zu Fragment für 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> 

die logcat spuckt wie folgt zusammen:

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)  

Ich bin mir nicht sicher, was ich von hier zu tun hätte, jede Hilfe wäre willkommen.

danke

+0

Es handelt sich um eine ClassCastException. Haben Sie den Aktivitätseintrag in der Datei "AndroidManifest.xml" überprüft? – JohnC

+0

Dank @JohnC habe ich das Manifest angeschaut und gesehen, dass es die Fragmente als Aktivitäten deklarierte (was zum Fehler führte). Ich habe sie aktualisiert und funktioniert jetzt perfekt. Vielen Dank. –

+0

Gern geschehen. Ich bin froh, dass es geholfen hat! – JohnC

Antwort

0

Was @JohnC sagte. Das Manifest wurde aktualisiert, um anzuzeigen, dass JobList.java keine Aktivität ist

Verwandte Themen