अभी मेरे ग्रिड व्यू को पॉप्युलेट करने के लिए मैं अपनी प्रत्येक एंड्रॉइड क्रियाकलापों (जो उनमें से अधिकतर हैं) पर विस्तारित बेसएडाप्टर क्लास का उपयोग कर रहा हूं।एंड्रॉइड: अलग-अलग वर्ग फ़ाइल पर बेसएडाप्टर और getLayoutInflater
इसे पढ़ने और बनाए रखने में आसान बनाने के लिए, मैं एक अलग वर्ग फ़ाइल में सभी बेसएडाप्टर कोड डालने की कोशिश कर रहा हूं।
getLayoutInflater के बाद से GridView भर दें, मैं LayoutInflater उपयोग कर रहा हूँ, और यहाँ जहां सामान मुश्किल हो जाता है है ...
() android.Activity
से आता है, यह सिर्फ चाल से काम नहीं चलेगा। मैंने अपने एडाप्टर.जावा (ग्रिड व्यूज़ को कक्षाबद्ध करने के लिए कक्षा) को विस्तारित गतिविधि वर्ग बनाने की कोशिश की, और फिर बेसएडाप्टर क्लास (जिस तरह से मैं इसे अभी करता हूं) बनाने के अंदर, लेकिन मैं इसे ठीक से काम करने में सक्षम नहीं हूं। एक
//MainClass.java
Adapter MyGridAdapter = new Adapter();
MyGrid = (GridView)findViewById(R.id.grid);
MyGrid.setAdapter(MyGridAdapter.new ImageAdapter(this, 6, nombreTema));
एप्लिकेशन रन है, लेकिन जब पॉप्युलेट करने के लिए कोशिश कर रहा है, मुझे मिल गया:,
//Adapter.java
package com.cimp.matitec;
import greendroid.app.GDActivity;
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;
public class Adapter extends GDActivity{
public class ImageAdapter extends BaseAdapter
{
Context MyContext;
int count;
String[] nombre;
public ImageAdapter(Context _MyContext, int n, String[] nombre)
{
MyContext = _MyContext;
count = n;
this.nombre = nombre;
}
public int getCount()
{
/* Set the number of element we want on the grid */
return count;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View MyView = convertView;
if (convertView == null)
{
/*we define the view that will display on the grid*/
//Inflate the layout
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
// Add The Text!!!
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText(nombre[position]+"");
// Add The Image!!!
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setImageResource(R.drawable.ic_launcher);
}
return MyView;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
}
बाहर से यह कॉल करने के लिए मैं निम्नलिखित है:
यहाँ कैसे Adapter.java लग रहा है NullPointerException getLayoutInflater()।
कोई जानता है कि मैं क्या खो रहा हूं, या इसे ठीक से कैसे काम करना है?
क्या जीडीएक्टिविटी 'गतिविधि' का विस्तार करती है? – Squonk
यूप, ओई परिप्रेक्ष्य से एक Greendroid गतिविधि –
है, एडाप्टर एक गतिविधि नहीं है, आपका कोड उचित नहीं है। आप सादगी की खोज करते हैं लेकिन समय और जटिलता के साथ समाप्त होते हैं, जिस तरह से Google अनुशंसित सबसे सही नहीं हो सकता है, लेकिन यह कम से कम आईएमओ सबसे उचित और कुशल है। – yorkw