我的代码有问题....当一个实例化项目时它会导致 NULL,我真的不知道是什么问题。有人可以帮忙吗?这种情况下的常见错误是什么?
public class DisplayActivity extends SherlockFragmentActivity {
final static int NUM_ITEMS = 9;
private final String TAG="links";
String keyword;
MyAdapter mAdapter;
ViewPager mPager;
static ArrayAdapter<String> adapter;
static View v;
static ListView tv;
static ArrayList<ArrayList<String>> items=new ArrayList<ArrayList<String>>();
searchTask exec;
static String sites[]={"http://www.record.pt/","http://www.dn.pt/inicio","http://www.jn.pt",
"http://www.publico.pt/?fullsite=true","http://www.abola.pt/?m=1",
"http://www.economico.sapo.pt/?version=desktop&redirect=/","http://www.ojogo.pt",
"http://www.expresso.sapo.pt","http://www.jornaldenegocios.pt","http://www.cmjornal.xl.pt"
};
@Override
public void onBackPressed(){
android.os.Process.killProcess(android.os.Process.myPid());
//DisplayActivity.this.finish();
}
@SuppressLint( "NewApi")
@Override
protected void onCreate(Bundle savedInstanceState){
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//getWindow().setWindowAnimations(17432586);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
searchTask exec=new searchTask();
exec.execute(sites);
mPager = (ViewPager)findViewById(R.id.pager);
mAdapter=new MyAdapter();
mPager.setAdapter(mAdapter);
Intent intent = getIntent();
keyword =intent.getStringExtra(MainActivity.EXTRA_INPUTKEY);
keyword.toLowerCase();
//Make sure we're running on Honeycomb or higher to use ActionBar APIs
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
//}
}
public static class MyAdapter extends PagerAdapter {
@Override
public void destroyItem(View collection, int position, Object view) {
//Util.Log("destroying" + position);
((ViewPager) collection).removeView((View) view);
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page=inflater.inflate(R.layout.activity_display_list, null);
ListView tv=(ListView) page.findViewById(android.R.id.list);
tv.setAdapter(adapter);
if (items.get(position).size() > 0){
for(int i=0; i < items.get(position).size();i++){
adapter.add(items.get(position).get(i));
}
}
((ViewPager) collection).addView(page, position);
adapter.notifyDataSetChanged();
return page;
}
public MyAdapter() {
super();
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
}
//***********************Fazer o add das listas aqui***************************
//Creates a new Task to execute the various getHtmlCode
public class searchTask extends AsyncTask<String, Elements, Elements> {
int number=0;
@Override
protected Elements doInBackground(String... params) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
for(int i=0 ; i < params.length;i++){
Elements aux=getHtmlCode(params[i]);
publishProgress(aux);
}
return null;
}
protected void onProgressUpdate(Elements... elem) {
int index=0;
ArrayList<String> aux=new ArrayList<String>();
for(Element link:elem[0]){
if(link.text().toLowerCase().contains(keyword)){
aux.add(index, link.text());
index++;
}
}
items.add(number, aux);
number++;
}
protected void onPostExecute(Elements aux){
//adapter.add("---------END---------");
//adapter.notifyDataSetChanged();
}
}
//parsing the html
public Elements getHtmlCode(String url) {
try {
Document doc = Jsoup.connect(url).get();
Elements content=doc.select("a[href]");
return content;
}
catch (IOException e) {
DisplayActivity.this.finish();
Log.e(TAG, "Failed to load HTML code", e);
// Also tell the user that something went wrong (keep it simple,
// no stacktraces):
//Toast.makeText(this, "Failed to load HTML code",
// Toast.LENGTH_SHORT).show();
}
return null;
}
}
09-09 21:02:38.239: E/AndroidRuntime(19656): FATAL EXCEPTION: main
09-09 21:02:38.239: E/AndroidRuntime(19656): java.lang.NullPointerException
09-09 21:02:38.239: E/AndroidRuntime(19656): at com.newsfinder.DisplayActivity$MyAdapter.instantiateItem(DisplayActivity.java:109)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.View.measure(View.java:8313)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.os.Looper.loop(Looper.java:130)
09-09 21:02:38.239: E/AndroidRuntime(19656): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-09 21:02:38.239: E/AndroidRuntime(19656): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 21:02:38.239: E/AndroidRuntime(19656): at java.lang.reflect.Method.invoke(Method.java:507)
09-09 21:02:38.239: E/AndroidRuntime(19656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-09 21:02:38.239: E/AndroidRuntime(19656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-09 21:02:38.239: E/AndroidRuntime(19656): at dalvik.system.NativeStart.main(Native Method)