2017-06-12 66 views
0
package com.darkweb.android.amityaaroh.HomeActivity; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.darkweb.android.amityaaroh.R; 

public class events extends Fragment { 

ListView listView; 

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

}; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.events); 

    initializeListView(); 
} 




private void initializeListView() { 

    listView = (ListView)getView().findViewById(R.id.listview); 

    final String[] items = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"}; 

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.textView2, items); 

    listView.setAdapter(arrayAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      String item = items[position]; 

      Toast.makeText(events.this.getActivity(), item+" is clicked", Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
} 

錯誤:我們可以把片段與appcomactivity 錯誤:(29,9)錯誤:無法找到符號方法的setContentView(int)的 錯誤:執行失敗的任務 ':應用程序:compileDebugJavaWithJavac'。Android的碎片與Appcompactivity

Compilation failed; see the compiler error output for details.

+1

刪除onCreate() – Akash

+0

中的setContentView方法在事件活動時應用程序未運行!如果我刪除setcontentview –

+0

刪除創建方法並調用你的initializeListView();方法在OnResume方法 – ashish

回答

0

移動和改變該

的ListView =(ListView中)getView()findViewById(R.id.listview)。

到onCreateView()

listView = rootView.findViewById(R.id.listview); 

刪除onCreate()方法

+0

didnt做這個替代 –

0

使用此代碼chnage的getview到getActivity(),您可以直接調用方法onCreateView

public class event extends Fragment { 
    ListView listView; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      View rootView = inflater.inflate(R.layout.events, container, false); 
      initializeListView(); 
      return rootView; 

     } 

     private void initializeListView() { 

      listView = (ListView)getActivity().findViewById(R.id.listview); 

      final String[] items = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"}; 


      ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String> (getActivity(), android.R.layout.simple_list_item_1, items); 
      listView.setAdapter(arrayAdapter); 

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        String item = items[position]; 

        Toast.makeText(events.this.getActivity(), item+" is clicked", Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 
    } 


} 
+0

應用程序的工作是強制關閉停止 –

+0

我改變代碼退房現在 –

+0

eError:(36,48)錯誤:發現ArrayAdapter(事件,INT的String []) 構造ArrayAdapter.ArrayAdapter沒有合適的構造(Context,int,int)不適用 (參數不匹配;事件不能轉換爲上下文) 構造函數ArrayAdapter.ArrayAdapter(Context,int,String [])不適用 (參數不匹配;事件不能轉換爲上下文) 構造函數ArrayAdapter.ArrayAdapter(Context,int,List )不適用 (參數不匹配;事件不能轉換爲上下文) –