2017-05-05 59 views
2

我在我的manifest.xml中遇到問題我嘗試了所有的東西,但我不明白爲什麼我得到「不能賦值給app.activity」的錯誤。我的展開式列表一個活動,而不是片段或其他東西。它只是擴展BaseExpandableListAdapter以顯示ExpandableListView。請幫幫我。 這裏是我屬於類:在manifest.xml中不能賦值給android.app.activity

  public class Expandablelist extends BaseExpandableListAdapter { 

     private Context _context; 
     private List<String> _listDataHeader; // header titles 
     // child data in format of header title, child title 
     private HashMap<String, List<String>> _listDataChild; 
     public Expandablelist(){ 

     } 

     public Expandablelist(Context context, List<String> listDataHeader, 
            HashMap<String, List<String>> listChildData) { 
      this._context = context; 
      this._listDataHeader = listDataHeader; 
      this._listDataChild = listChildData; 
     } 

     @Override 
     public Object getChild(int groupPosition, int childPosititon) { 
      return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
        .get(childPosititon); 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
     } 

     @Override 
     public View getChildView(int groupPosition, final int childPosition, 
           boolean isLastChild, View convertView, ViewGroup parent) { 

      final String childText = (String) getChild(groupPosition, childPosition); 

      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) this._context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.list_item, null); 
      } 

      TextView txtListChild = (TextView) convertView 
        .findViewById(R.id.lblListItem); 

      txtListChild.setText(childText); 
      return convertView; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
        .size(); 
     } 

     @Override 
     public Object getGroup(int groupPosition) { 
      return this._listDataHeader.get(groupPosition); 
     } 

     @Override 
     public int getGroupCount() { 
      return this._listDataHeader.size(); 
     } 

     @Override 
     public long getGroupId(int groupPosition) { 
      return groupPosition; 
     } 

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
           View convertView, ViewGroup parent) { 
      String headerTitle = (String) getGroup(groupPosition); 
      if (convertView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) this._context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = infalInflater.inflate(R.layout.list_group, null); 
      } 

      TextView lblListHeader = (TextView) convertView 
        .findViewById(R.id.lblListHeader); 
      lblListHeader.setTypeface(null, Typeface.BOLD); 
      lblListHeader.setText(headerTitle); 

      return convertView; 
     } 

     @Override 
     public boolean hasStableIds() { 
      return false; 
     } 

     @Override 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 
     } 

這裏是我的mainactivity.java:

 public class MainActivity extends AppCompatActivity 
      implements NavigationView.OnNavigationItemSelectedListener { 

     Expandablelist listAdapter; 
     // ExpandableListView expListView; 
      List<String> listDataHeader; 
      HashMap<String, List<String>> listDataChild; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       // auf anderes xml layout zugreifen 



      setContentView(R.layout.activity_main); 


      // LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 
      // inflater.inflate(R.layout.activity_expandablelist,null); 

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      final ImageButton generaldiscriptionbutton = (ImageButton) findViewById(R.id.imageButton); 
      setSupportActionBar(toolbar); 
      checkFirstRun(); 



      View expandablelistview = getLayoutInflater().inflate(R.layout.activity_expandablelist, null, false); 
      ExpandableListView expListView = (ExpandableListView) expandablelistview.findViewById(R.id.lvExp); 


      // get the listview 
      // expListView = (ExpandableListView) findViewById(R.id.lvExp); 


      // preparing list data 
      prepareListData(); 

      listAdapter = new Expandablelist(this, listDataHeader, listDataChild); 

      // setting list adapter 
      expListView.setAdapter(listAdapter); 
      private void prepareListData() { 
      listDataHeader = new ArrayList<String>(); 
      listDataChild = new HashMap<String, List<String>>(); 

      // Adding child data 
      listDataHeader.add("Top 250"); 
      listDataHeader.add("Now Showing"); 
      listDataHeader.add("Coming Soon.."); 

      // Adding child data 
      List<String> top250 = new ArrayList<String>(); 
      top250.add("The Shawshank Redemption"); 
      top250.add("The Godfather"); 
      top250.add("The Godfather: Part II"); 
      top250.add("Pulp Fiction"); 
      top250.add("The Good, the Bad and the Ugly"); 
      top250.add("The Dark Knight"); 
      top250.add("12 Angry Men"); 

      List<String> nowShowing = new ArrayList<String>(); 
      nowShowing.add("The Conjuring"); 
      nowShowing.add("Despicable Me 2"); 
      nowShowing.add("Turbo"); 
      nowShowing.add("Grown Ups 2"); 
      nowShowing.add("Red 2"); 
      nowShowing.add("The Wolverine"); 

      List<String> comingSoon = new ArrayList<String>(); 
      comingSoon.add("2 Guns"); 
      comingSoon.add("The Smurfs 2"); 
      comingSoon.add("The Spectacular Now"); 
      comingSoon.add("The Canyons"); 
      comingSoon.add("Europa Report"); 

      listDataChild.put(listDataHeader.get(0), top250); // Header, Child data 
      listDataChild.put(listDataHeader.get(1), nowShowing); 
      listDataChild.put(listDataHeader.get(2), comingSoon); 
     } 
public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_basics) { 
      // Handle the camera action 
      Intent i = new Intent(MainActivity.this, Explain.class); 
      startActivity(i); 


     } else if (id == R.id.nav_explaination) { 

     } else if (id == R.id.nav_materials) { 

      Intent i = new Intent(MainActivity.this, Expandablelist.class); 
      startActivity(i); 

     } 

這是我的manifest.xml在我Expandableist活動聲明:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <!-- <activity android:name=".Explain"></activity> --> 

    <activity 
     android:name=".Explain" 
     android:parentActivityName=".MainActivity"> 

     <!-- The meta-data tag is required if you support API level 15 and lower --> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".MainActivity" /> 
    </activity> 
    <!-- <activity android:name=".BasicMaterials"></activity> --> 

    <activity 
     android:name=".BasicMaterials" 
     android:parentActivityName=".Explain"> 


     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".Explain" /> 
    </activity> 

    <activity 
     android:name=".Generaldiscription" 
     android:label="@string/title_activity_generaldiscription" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme.NoActionBar" /> 
    <activity 
     android:name=".Expandablelist"> // here is the error 

    </activity> 
</application> 

問題:爲什麼我在android清單xml中出現錯誤?我該如何解決這個問題。該應用程序的作品,但是當我點擊邊欄上的R.id.nav.materials,它崩潰。現在看來似乎不承認我的expandablelist作爲一項活動,我不知道爲什麼

這裏是我的崩潰日誌:

FATAL EXCEPTION: main 
                       Process: com.example.cem.fahrzeugewaschen, PID: 25196 
                       java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.project/com.example.user.project.Expandablelist}: java.lang.ClassCastException: com.example.user.project.Expandablelist cannot be cast to android.app.Activity 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
                        at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:145) 
                        at android.app.ActivityThread.main(ActivityThread.java:5951) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:372) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 
                       Caused by: java.lang.ClassCastException: com.example.cem.fahrzeugewaschen.Expandablelist cannot be cast to android.app.Activity 
                        at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)  
                        at android.app.ActivityThread.access$900(ActivityThread.java:177)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:145)  
                        at android.app.ActivityThread.main(ActivityThread.java:5951)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at java.lang.reflect.Method.invoke(Method.java:372)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)  
+0

'我的expandablelist是一個活動'不,它不是**。這是一個** BaseExpandableListAdapter **。因爲它只是擴展BaseExpandableListAdapter而不是擴展** Activity **。 –

+0

啊,我明白了,怎麼解決? – Blnpwr

+0

帖子崩潰詳細信息 –

回答

2
Intent i = new Intent(MainActivity.this, Expandablelist.class); 
startActivity(i); 

這是不對的。第二個參數必須是一個Activity.class

要顯示列表,請創建另一個活動,在其佈局中顯示Expandablelist

請參閱Android ExpandableListView Sample

+0

謝謝,請問您能否舉個例子,因爲我是這個問題的新手? – Blnpwr

+0

'創建另一個活動'或一個片段(所以你不必在清單中聲明它)。 –

+0

使用教程鏈接更新答案。 @Blnpwr – VishnuSP