2015-06-29 79 views
1

我非常新到Android工作室和視頻教程還在學習。我發現畢加索,並希望將它用於我的項目,但是當我嘗試應用/在列表視圖中練習時,我似乎並沒有得到它,即時通訊有一個錯誤,說Target must not be null。我嘗試瞭解決方案,但仍然沒有。對不起,我的英語不好。這裏是我的代碼,請幫助我。畢加索Android的目標不能爲空

class MyAdapter extends ArrayAdapter<String> { 

Context context; 

public MyAdapter(Context context, int row_layout, String[] values) { 
    super(context, R.layout.row_layout2, values); 
} 



@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageHolder holder = null; 
    holder = new ImageHolder(); 

    LayoutInflater theInflater = LayoutInflater.from(getContext()); 
    View theView = theInflater.inflate(R.layout.row_layout2, parent, false); 
    String tvShow = getItem(position); 

    TextView theTextView = (TextView) theView.findViewById(R.id.textView1); 

    theTextView.setText(tvShow); 

    holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageView1); 

    convertView.setTag(holder); 

    Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon); //ERROR HERE 

    return theView; 
} 

static class ImageHolder 
{ 
    ImageView imageIcon; 
} 
} 

MainActivity

public class MainActivity extends AppCompatActivity { 

static class ImageHolder { 
    ImageView imageIcon; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String[] favoriteShows = { 
      "Hannibal", "Sherlock", "Supernatural" 
    }; 

    //ImageHolder holder = new ImageHolder(); 

    ListAdapter theAdapter = new MyAdapter(this, R.layout.row_layout, favoriteShows); 

    ListView theListView = (ListView) findViewById(R.id.theListView); 

    theListView.setAdapter(theAdapter); 

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      String tvShowPicked= "You selected " + 
        String.valueOf(adapterView.getItemAtPosition(position)); 

      Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

伊夫ALSE試圖把MainActivity畢加索線,看看是否有什麼變化。

holder.imageIcon = (ImageView) findViewById(R.id.imageView1); 

    ImageView imageView = (ImageView) findViewById(R.id.imageView1); 

    Picasso.with(this) 
      .load(R.drawable.sample4) 
      .into(holder.imageIcon); 

我row_layout2.xml

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="1dp" 
    android:layout_marginTop="1dp" 
    android:layout_marginRight="1dp" 
    android:src="@drawable/sample3" 
    android:id="@+id/imageView1" 
    android:adjustViewBounds="true" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView1" 
    android:textSize="10sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:gravity="left" 
    android:paddingLeft="15dp"> 
</TextView> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView2" 
    android:textSize="10sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:gravity="left|bottom" 
    android:padding="15dp" 
    android:text="ADDRESS HERE"> 
</TextView> 

我activity_main.xml中

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/theListView"> 

    </ListView> 

錯誤日誌:

06-29 21:06:13.990 1288-1288/com.erlawkward.albarol.listviewexample E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erlawkward.albarol.listviewexample/com.erlawkward.albarol.listviewexample.MainActivity}: java.lang.IllegalArgumentException: Target must not be null. 

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 

at android.app.ActivityThread.access$600(ActivityThread.java:141) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 

at android.os.Handler.dispatchMessage(Handler.java:99) 

at android.os.Looper.loop(Looper.java:137) 

at android.app.ActivityThread.main(ActivityThread.java:5041) 

at java.lang.reflect.Method.invokeNative(Native Method) 

at java.lang.reflect.Method.invoke(Method.java:511) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 

at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.IllegalArgumentException: Target must not be null. 

at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618) 

at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601) 

at com.erlawkward.albarol.listviewexample.MainActivity.onCreate(MainActivity.java:59) 

at android.app.Activity.performCreate(Activity.java:5104) 

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
             
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
             
at android.app.ActivityThread.access$600(ActivityThread.java:141) 
             
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
             
at android.os.Handler.dispatchMessage(Handler.java:99) 
             
at android.os.Looper.loop(Looper.java:137) 
             
at android.app.ActivityThread.main(ActivityThread.java:5041) 
             
at java.lang.reflect.Method.invokeNative(Native Method) 
             
at java.lang.reflect.Method.invoke(Method.java:511) 
             
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
             
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
             
at dalvik.system.NativeStart.main(Native 

法)

+0

郵政佈局文件 – omainegra

回答

1

在你的適配器

Picasso.with(getContext()).load(R.drawable.sample4).into(holder.imageIcon); 

編輯

而且你使用的是ViewHolder模式以錯誤的方式取代

Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon); 

。使用它的全部意義在於每次調用getView時避免膨脹佈局。我編輯的方法遵循

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

    ImageHolder holder; 

    if (convertView == null){ 
     LayoutInflater theInflater = LayoutInflater.from(getContext()); 
     convertView = theInflater.inflate(R.layout.row_layout2, parent, false); 

     holder = new ImageHolder(); 
     holder.textView = (TextView) convertView.findViewById(R.id.textView1); 
     holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageView1); 

     convertView.setTag(holder); 
    } 
    else{ 
     holder = (ImageHolder) convertView.getTag(); 
    } 

    holder.textView.setText(getItem(position)); 

    //ERROR FIXED 
    Picasso.with(getContext()).load(R.drawable.sample4).into(holder.imageIcon); 

    return convertView; 
} 

的持有人

static class ImageHolder 
{ 
    ImageView imageIcon; 
    TextView textView; 
} 

而且在的onCreate活動

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String[] favoriteShows = { 
      "Hannibal", "Sherlock", "Supernatural" 
    }; 

    ListAdapter theAdapter = new MyAdapter(this, R.layout.row_layout2, favoriteShows); 

    ListView theListView = (ListView) findViewById(R.id.theListView); 
    theListView.setAdapter(theAdapter); 

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      String tvShowPicked = "You selected " + 
        String.valueOf(adapterView.getItemAtPosition(position)); 

      Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
+0

仍然有同樣的錯誤。我的holder.imageIcon爲空,我不知道爲什麼。 – ErlAl

+0

@ErlAl您是否用我的'getView'方法替換了您的'getView'方法?同時檢查這兩個方案中的錯誤是否相同 – omainegra

+0

是的先生,我已將其替換並仍然有相同的錯誤。 – ErlAl

1

原因是您的context爲空。

嘗試初始化您的上下文。

public MyAdapter(Context context, int row_layout, String[] values) { 
    super(context, R.layout.row_layout2, values); 
    this.context=context; 
} 
+0

仍具有相同的錯誤的代碼。 Picasso.with(this.context).load(的getItem(R.drawable.sample4))代入(holder.imageIcon)。 我.into(價值)仍然是空 – ErlAl

+0

@ omainegra的答案應該解決您的問題,如果沒有,那麼有可能是ID與你的錯配layout.I的意思是說imageView1可能不row_layout2.xml文件存在。請雙核實。 – Soham

+0

這裏是我的ImageView ErlAl

0

爲什麼使用Picasso將drawable加載到ImageView中? 只需使用的ImageView的setImageResource()方法。

+0

我已經嘗試使用它,並使用不同的圖像尺寸,但到目前爲止,我不喜歡輸出。 我希望我放置的所有圖像的大小與設備的大小相同。如果你能幫我或給我一個小費,請 – ErlAl