2016-08-02 2 views
1

Как решить эту ошибку во время выполнения? СпасибоFragment Custom Listview: получение ошибки времени выполнения

Ошибка FATAL ИСКЛЮЧЕНИЕ: Основной процесс: com.example.arunabha.eshopping, PID: 1673 java.lang.RuntimeException: Ваше содержание должно быть ListView, чей идентификатор атрибута «android.R .id.list»

BlankFragment1.java

import android.content.Context; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.Toast; 
import java.util.ArrayList; 
public class BlankFragment1 extends ListFragment { 
ListView mobileLV; 
Adapter adapter; 
ArrayList<SingleRowForMobile> list; 
Context mContex; 
public BlankFragment1() { 
    // Required empty public constructor 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_blank_fragment1, container, false); 
    mobileLV = (ListView) view.findViewById(R.id.mobileLV); 
    mContex = getActivity().getApplicationContext(); 
    list = new ArrayList<SingleRowForMobile>(); 
    Resources res = mContex.getResources(); 
    int images[] = {R.drawable.apple_logo, R.drawable.blackberry_logo, R.drawable.samsung_logo, R.drawable.microsoft_logo, R.drawable.gionee_logo, R.drawable.micromax_logo, R.drawable.lenovo, R.drawable.asus_logo}; 
    String names[] = res.getStringArray(R.array.name_for_mobile); 
    for (int i = 0; i < 8; i++) { 
     list.add(new SingleRowForMobile(names[i], images[i])); 

    } 
    adapter = new Adapter(mContex, list); 
    mobileLV.setAdapter(adapter); 
    mobileLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Toast.makeText(getContext(), "" + i, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return view; 
} 

}

SingleRowForMobile.java

import android.graphics.Bitmap; 

public class SingleRowForMobile { 

    int images; 
    String names; 

    SingleRowForMobile(String names, int images) { 
     this.images = images; 
     this.names = names; 
    } 

    public int getImages() { 
     return images; 
    } 

    public void setImages(int images) { 
     this.images = images; 
    } 

    public String getNames() { 
     return names; 
    } 

    public void setNames(String names) { 
     this.names = names; 
    } 

} 

Adapter.java

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.arunabha.eshopping.R; 
import com.example.arunabha.eshopping.SingleRowForMobile; 

import java.util.ArrayList; 


public class Adapter extends BaseAdapter { 

    ArrayList<SingleRowForMobile> list; 
    Context context; 
    Adapter(Context c, ArrayList<SingleRowForMobile> listInput){ 
     context=c; 
     list= listInput; 

    } 


    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return list.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.single_row,viewGroup,false); 

     TextView names = (TextView) row.findViewById(R.id.textView); 
     ImageView images = (ImageView) row.findViewById(R.id.img); 

     SingleRowForMobile temp = list.get(i); 
     names.setText(temp.names); 
     images.setImageResource(temp.images); 

     return row; 
    } 
} 

fragment_blank_fragment1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 

    tools:context="com.example.arunabha.eshopping.BlankFragment1"> 

    <ListView 
     android:id="@+id/mobileLV" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

single_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <ImageView 

     android:id="@+id/img" 
     android:layout_width="200dp" 
     android:layout_height="200dp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:gravity="center" 
      android:text="India" 
      android:textSize="25sp" /> 


    </LinearLayout> 
</LinearLayout> 

ответ

1

Измените свой идентификатор ListView из mobileLV в android.R.id.list'

+0

это изменение в классе 'java'? что будет для 'xml' .. пожалуйста, очистите. –

+0

для 'java' и' xml' –

+0

Спасибо lottttttttttttttt. :) –

Смежные вопросы