2015-06-01 183 views
0
package com.example.layout.layout; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.HashMap; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 

import org.w3c.dom.Text; 


/** 
* A placeholder fragment containing a simple view. 
*/ 
public class MovieInfoFile extends Fragment { 
    public static final String ARG_OPTION="argument_option"; 
    HashMap[]items=new HashMap[30]; 
    int i; 
    public ImageView image; 
    TextView name; 
    TextView des; 
    TextView stars; 
    TextView year; 
    TextView rating; 
    public static MovieInfoFile newInstance(int option) 
    { 
     MovieInfoFile fragment=new MovieInfoFile(); 
     Bundle args=new Bundle(); 

     args.putInt(ARG_OPTION,option); 
     fragment.setArguments(args); 



     return fragment; 

    } 

    public MovieInfoFile() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view= inflater.inflate(R.layout.movieinfolayout, container, false); 
     Button loadnext=(Button) view.findViewById(R.id.next); 
     image=(ImageView) view.findViewById((R.id.image1)); 
     name=(TextView) view.findViewById(R.id.nameans); 
     des=(TextView) view.findViewById(R.id.desans); 
     stars=(TextView) view.findViewById(R.id.starans); 
     year=(TextView) view.findViewById(R.id.yearans); 
     rating=(TextView) view.findViewById(R.id.ratingans); 
     enter code hereView.OnClickListener aButtonChangeImageListener=new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(v.getId()==R.id.next) 
       { 

        int id=(Integer)items[1].get("image"); 
        image.setImageResource(id); 
       } 



      } 
     }; 





     return view; 
    } 
} 



Here is the layout (XML file) 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:background="@drawable/baground" 
    android:weightSum="1"> 
    <ImageView 
     android:layout_width="315dp" 
     android:layout_height="200dp" 
     android:layout_gravity="center" 
     android:src="@drawable/avatar" 
     android:id="@+id/image1" 
     android:layout_weight="0.53" /> 
    <TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:layout_gravity="center_horizontal" 
     android:id="@+id/table1"> 
     <TableRow> 
      <TextView 
       android:text="Movie Name: " 
       android:textStyle="bold" 
       android:id="@+id/name" 

       /> 
      <TextView 
       android:text="Movie Name: " 
       android:textStyle="bold" 
       android:id="@+id/nameans" 

       /> 
      </TableRow> 
     <TableRow> 
      <TextView 
       android:text="Description: " 
       android:textStyle="bold" 
       android:id="@+id/des" 

       /> 
      <TextView 
       android:text="Description: " 
       android:textStyle="bold" 
       android:id="@+id/desans" 

       /> 
     </TableRow> 
     <TableRow> 
      <TextView 
       android:text="Stars " 
       android:textStyle="bold" 
       android:id="@+id/star" 

       /> 
      <TextView 
       android:text="Stars " 
       android:textStyle="bold" 
       android:id="@+id/starans" 

       /> 
     </TableRow> 
     <TableRow> 
      <TextView 
       android:text="Year" 
       android:textStyle="bold" 
       android:id="@+id/year" 


       /> 
      <TextView 
       android:text="Year" 
       android:textStyle="bold" 
       android:id="@+id/yearans" 

       /> 
     </TableRow> 

     <TableRow> 
      <TextView 
       android:text="Rating" 
       android:textStyle="bold" 
       android:id="@+id/rating" 

       /> 
      <TextView 
       android:text="Rating" 
       android:textStyle="bold" 
       android:id="@+id/ratingans" 

       /> 
     </TableRow> 


    </TableLayout> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="LoadPrev" 
     android:id="@+id/prev" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Loadnext" 
     android:layout_gravity="center_horizontal" 
     android:id="@+id/next"/> 
</LinearLayout> 

我想改變一個片段內的按鈕單擊按鈕上動態圖像視圖。但是當我這樣做的時候,它給了我錯誤,「不幸的是你的應用程序已經停止了」。任何人都可以在這裏幫助我,並告訴我在片段中點擊按鈕時動態修改視圖的正確方法。修改圖像視圖按鈕上點擊按鈕動態

+4

請留言發生這種情況時的堆棧跟蹤 –

回答

1

按鈕加載下一(我猜一下這個形象應該改變後),所以通過aButtonChangeImageListener到按鈕loadNext這樣的:

loadNext.setOnClickListener(aButtonChangeImageListener); 

返回查看前行