2016-10-29 87 views
-1

應用程序不工作我不知道原因。Android應用程序 - ListView不工作

CustomView.java

public class CustomView extends Activity { 
     ListView list; 
     String titles[] = getResources().getStringArray(R.array.titles); 
     int images[] = getResources().getIntArray(R.array.movies); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    CustomList adapter = new CustomList(CustomView.this); 
    list=(ListView)findViewById(R.id.listview); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getBaseContext(),titles[+position],Toast.LENGTH_LONG).show(); 
     } 
    }); 

} 

public class CustomList extends ArrayAdapter<String>{ 
    final Activity context; 
    public CustomList(Activity context){ 
     super(context,R.layout.listitem,titles); 
     this.context = context; 

    } 


    @Override 
    public View getView(int pos, View convertView, ViewGroup parent){ 

     double randomValue = Math.random(); 
     double randomYear = Math.random(); 
     LayoutInflater inflater =context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.listitem,null,true); 
     ImageView imageView = (ImageView)rowView.findViewById(R.id.image); 
     TextView title = (TextView)rowView.findViewById(R.id.title); 
     TextView rating = (TextView)rowView.findViewById(R.id.rating); 
     TextView genre = (TextView)rowView.findViewById(R.id.genre); 
     TextView year = (TextView)rowView.findViewById(R.id.year); 

     title.setText(titles[pos]); 
     imageView.setImageResource(images[pos]); 
     rating.setText(((int)randomValue*9)+0+"."+pos) ; 
     genre.setText("DRAMA"); 
     year.setText(((int)randomYear+1960)+1930); 


     return rowView; 
    } 

    } 
} 

佈局文件 main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 

tools:context="com.example.kim.customlistview.CustomView"> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/listview" /> 
</RelativeLayout> 

列表項listitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#eeeeee" 
android:padding="8dp" 
> 

<ImageView 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:id="@+id/image" 
    android:layout_alignParentLeft="true"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/title" 
    android:layout_alignTop="@+id/image" 
    android:layout_toRightOf="@+id/image" 
    android:textSize="17dp" 

    /> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/rating" 
    android:layout_below="@+id/title" 
    android:layout_marginTop="1dip" 
    android:layout_toRightOf="@id/image" 
    android:textSize="15dip" 
    /> 


<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/genre" 
    android:layout_below="@+id/rating" 
    android:layout_marginTop="5dp" 
    android:layout_toRightOf="@id/image" 
    android:textSize="13dip" 

    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/year" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:textSize="12dip" 
    /> 

</RelativeLayout> 

資源string.xml

<resources> 
<string name="app_name">CustomListView</string> 
<string-array name="titles"> 
    <item>"The Wizard of Oz (1939)"</item> 
    <item>"Citizen kane (1950)"</item> 
    <item>"A Hard Day's Night (1934)"</item> 
    <item>"A Funny (1959)"</item> 
    <item>"HELLO (1943)"</item> 
</string-array> 
</resources> 

array.xml

<?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    <array name="movies"> 
     <item>@drawable/movie1</item> 
     <item>@drawable/movie2</item> 
     <item>@drawable/movie3</item> 
     <item>@drawable/movie4</item> 
     <item>@drawable/movie5</item> 
    </array> 
    </resources> 

堆棧跟蹤:

10-29 16:25:26.221 9745-9745/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.example.kim.customlistview, PID: 9745 
               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kim.customlistview/com.example.kim.customlistview.CustomView}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 



Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
               at android.content.ContextWrapper.getResources(ContextWrapper.java:92) 

回答

0

Intanziate titles[]images[]和insde onCreate()方法,否則調用getResources()所需的上下文爲空。

0

添加此行到onCreate方法活動

String titles[] = getResources().getStringArray(R.array.titles); 
    int images[] = getResources().getIntArray(R.array.movies); 

你的問題就迎刃而解了

+0

最後字符串的標題[] = getResources()getStringArray(R.array.titles); int images [] = getResources()。getIntArray(R.array.movi​​es);添加到onCreate()但不工作 –

0

與您的代碼的問題是,你正在使用<array>標籤保存電影的圖像。即<item>@drawable/movie1</item>,它實際上保存了該圖像的integer引用。

<array name="movies"> 
</array> 

您有<integer-array>更換<array>

<integer-array name="movies"> 
</integer-array> 
+0

對不起,沒有工作 –

0

這裏充滿溶液:

public class CustomView extends Activity { 


int images[] = {R.drawable.movie1, R.drawable.movie2, R.drawable.movie3, R.drawable.movie4, R.drawable.movie5}; 
List<String> movies; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.stackover); 
    movies = new ArrayList<>(); 
    movies.add(0, "The Wizard of Oz (1939)"); 
    movies.add(1, "Citizen kane (1950)"); 
    movies.add(2, "A Hard Day's Night (1934)"); 
    movies.add(3, "A Funny (1959)"); 
    movies.add(4, "HELLO (1943)"); 

    listView = (ListView) findViewById(R.id.demoList); 
    listView.setAdapter(new CustomList(StackOver.this, movies, images)); 
} 

public class CustomList extends BaseAdapter { 

    List<String> movie; 
    int[] image; 
    private Context context; 

    public CustomList(Context context, List<String> movie, int[] image) { 
     this.context = context; 
     this.movie = movie; 
     this.image = image; 

    } 

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

    @Override 
    public Object getItem(int position) { 
     return movie.get(position); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) context 
        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.listitem_stack, null); 
     } 



     ImageView imageView = (ImageView) convertView.findViewById(R.id.image); 
     TextView title = (TextView) convertView.findViewById(R.id.title); 
     TextView rating = (TextView) convertView.findViewById(R.id.rating); 
     TextView genre = (TextView) convertView.findViewById(R.id.genre); 
     TextView year = (TextView) convertView.findViewById(R.id.year); 

     title.setText(movie.get(position)); 
     imageView.setImageResource(image[position]); 
     rating.setText("4.5"); 
     genre.setText("DRAMA"); 
     year.setText("1990"); 

     return convertView; 
    } 
    } 
} 
+0

thx,但我想通過數組在xml源數據(圖像,電影)。並在Actvity Class中調用,如「String [] titles = getResources()。getStringArray(R.array.titles)」 –

相關問題