2015-07-20 86 views
-1

我正在使用Parse,并且我正在跟着tutorial创建包含图像和文本的自定义listview。我能够在我的列表视图中检索图像和文本。但是,当我点击一个listview项时,它会抛出一个空指针异常,而不是重定向到一个新的活动并显示特定的结果。我仍然不确定哪里可能出错。Android Activity - NullPointerException(无法实例化活动ComponentInfo)

异常消息:

FATAL EXCEPTION: 
    java.lang.RuntimeException: Unable to instantiate activityComponentInfo{com.androidbegin.parselistviewimgtxt/com.androidbegin.parselistviewimgtxt.SingleItemView}: java.lang.NullPointerException. 
    Caused by: java.lang.NullPointerException at com.androidbegin.parselistviewimgtxt.FileCache.<init>(FileCache.java:18) 
    at com.androidbegin.parselistviewimgtxt.ImageLoader.<init>(ImageLoader.java:35) 
    07-20 01:54:24.114: E/AndroidRuntime(1313):  at com.androidbegin.parselistviewimgtxt.SingleItemView.<init>(SingleItemView.java:16) 

下面是代码:

MainActivity.java

public class MainActivity extends Activity { 
// Declare Variables 
ListView listview; 
List<ParseObject> ob; 
ProgressDialog mProgressDialog; 
ListViewAdapter adapter; 
private List<WorldPopulation> worldpopulationlist = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Get the view from listview_main.xml 
    setContentView(R.layout.listview_main); 
    // Execute RemoteDataTask AsyncTask 
    new RemoteDataTask().execute(); 
} 

// RemoteDataTask AsyncTask 
private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(MainActivity.this); 

     // Set progressdialog title 

     // Set progressdialog message 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Create the array 
     worldpopulationlist = new ArrayList<WorldPopulation>(); 
     try { 
      // Locate the class table named "Country" in Parse.com 
      ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
        "Country"); 
      // Locate the column named "ranknum" in Parse.com and order list 
      // by ascending 
      query.orderByAscending("ranknum"); 
      ob = query.find(); 
      for (ParseObject country : ob) { 
       // Locate images in flag column 
       ParseFile image = (ParseFile) country.get("flag"); 

       WorldPopulation map = new WorldPopulation(); 
       map.setRank((String) country.get("rank")); 
       map.setCountry((String) country.get("country")); 
       map.setPopulation((String) country.get("population")); 
       map.setFlag(image.getUrl()); 
       worldpopulationlist.add(map); 
      } 
     } catch (ParseException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.listview); 
     // Pass the results into ListViewAdapter.java 
     adapter = new ListViewAdapter(MainActivity.this, 
       worldpopulationlist); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
     // Close the progressdialog 
     mProgressDialog.dismiss(); 
    } 


} 
} 

SingleItemView.java

public class SingleItemView extends Activity { 
    // Declare Variables 
    String rank; 
    String country; 
    String population; 
    String flag; 
    String position; 
    ImageLoader imageLoader = new ImageLoader(this); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Get the view from singleitemview.xml 
    setContentView(R.layout.singleitemview); 

    // Locate the TextViews in singleitemview.xml 
    TextView txtrank = (TextView) findViewById(R.id.rank); 
    TextView txtcountry = (TextView) findViewById(R.id.country); 
    TextView txtpopulation = (TextView) findViewById(R.id.population); 


    // Locate the ImageView in singleitemview.xml 
    ImageView imgflag = (ImageView) findViewById(R.id.flag); 


    Intent i = getIntent(); 
    // Get the result of rank 
    rank = i.getStringExtra("rank"); 
    // Get the result of country 
    country = i.getStringExtra("country"); 
    // Get the result of population 
    population = i.getStringExtra("population"); 
    // Get the result of flag 
    flag = i.getStringExtra("flag"); 


    // Set results to the TextViews 
    txtrank.setText(rank); 
    txtcountry.setText(country); 
    txtpopulation.setText(population); 

    // Capture position and set results to the ImageView 
    // Passes flag images URL into ImageLoader.class 
    imageLoader.DisplayImage(flag, imgflag); 
} 

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter { 

// Declare Variables 
Context context; 
LayoutInflater inflater; 
ImageLoader imageLoader; 
private List<WorldPopulation> worldpopulationlist = null; 
private ArrayList<WorldPopulation> arraylist; 

public ListViewAdapter(Context context, 
     List<WorldPopulation> worldpopulationlist) { 
    this.context = context; 
    this.worldpopulationlist = worldpopulationlist; 
    inflater = LayoutInflater.from(context); 
    this.arraylist = new ArrayList<WorldPopulation>(); 
    this.arraylist.addAll(worldpopulationlist); 
    imageLoader = new ImageLoader(context); 
} 

public class ViewHolder { 
    TextView rank; 
    TextView country; 
    TextView population; 
    ImageView flag; 
} 

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

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

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

public View getView(final int position, View view, ViewGroup parent) { 
    final ViewHolder holder; 
    if (view == null) { 
     holder = new ViewHolder(); 
     view = inflater.inflate(R.layout.listview_item, null); 
     // Locate the TextViews in listview_item.xml 
     holder.rank = (TextView) view.findViewById(R.id.rank); 
     holder.country = (TextView) view.findViewById(R.id.country); 
     holder.population = (TextView) view.findViewById(R.id.population); 
     // Locate the ImageView in listview_item.xml 
     holder.flag = (ImageView) view.findViewById(R.id.flag); 
     view.setTag(holder); 
    } else { 
     holder = (ViewHolder) view.getTag(); 
    } 
    // Set the results into TextViews 
    holder.rank.setText(worldpopulationlist.get(position).getRank()); 
    holder.country.setText(worldpopulationlist.get(position).getCountry()); 
    holder.population.setText(worldpopulationlist.get(position) 
      .getPopulation()); 
    // Set the results into ImageView 
    imageLoader.DisplayImage(worldpopulationlist.get(position).getFlag(), 
      holder.flag); 
    // Listen for ListView Item Click 
    view.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Send single item click data to SingleItemView Class 
      Intent intent = new Intent(context, SingleItemView.class); 
      // Pass all data rank 
      intent.putExtra("rank", 
        (worldpopulationlist.get(position).getRank())); 
      // Pass all data country 
      intent.putExtra("country", 
        (worldpopulationlist.get(position).getCountry())); 
      // Pass all data population 
      intent.putExtra("population", 
        (worldpopulationlist.get(position).getPopulation())); 
      // Pass all data flag 
      intent.putExtra("flag", 
        (worldpopulationlist.get(position).getFlag())); 
      // Start SingleItemView Class 
      context.startActivity(intent); 
     } 
    }); 
    return view; 
} 

我试图

if (getIntent().getStringExtra("rank") != null && (getIntent().getStringExtra("country") != null)&& (getIntent().getStringExtra("population") != null)&& (getIntent().getStringExtra("flag") != null)){ 

     rank = bundle.getString("rank"); 
     // Get the result of country 
     country = bundle.getString("country"); 
     // Get the result of population 
     population = bundle.getString("population"); 
     // Get the result of flag 
     flag = bundle.getString("flag"); 

    } 
+0

你在'ob'中设置了哪个值?当你在'doInBackground()'方法的'ob'中调用这些方法时,ob甚至没有被初始化。因此为空。 –

+0

check getIntent()。你在那里得到空 – NovusMobile

+0

我试图在getIntent()检查空值,但它仍然会抛出相同的异常 – Enovyne

回答

0

我想你应该把的ImageLoader imageLoader初始化中SingleItemViewonCreate以上方法你使用它之前imageLoader.DisplayImage(flag, imgflag);地方。

它可能看起来像这样

public class SingleItemView extends Activity { 
    // Declare Variables 
    String rank; 
    String country; 
    String population; 
    String flag; 
    String position; 
    ImageLoader imageLoader; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from singleitemview.xml 
     setContentView(R.layout.singleitemview); 

     // Locate the TextViews in singleitemview.xml 
     TextView txtrank = (TextView) findViewById(R.id.rank); 
     TextView txtcountry = (TextView) findViewById(R.id.country); 
     TextView txtpopulation = (TextView) findViewById(R.id.population); 


     // Locate the ImageView in singleitemview.xml 
     ImageView imgflag = (ImageView) findViewById(R.id.flag); 


     Intent i = getIntent(); 
     // Get the result of rank 
     rank = i.getStringExtra("rank"); 
     // Get the result of country 
     country = i.getStringExtra("country"); 
     // Get the result of population 
     population = i.getStringExtra("population"); 
     // Get the result of flag 
     flag = i.getStringExtra("flag"); 


     // Set results to the TextViews 
     txtrank.setText(rank); 
     txtcountry.setText(country); 
     txtpopulation.setText(population); 

     // Capture position and set results to the ImageView 
     // Passes flag images URL into ImageLoader.class 
     imageLoader = new ImageLoader(this); 
     imageLoader.DisplayImage(flag, imgflag); 
    } 

之前onCreateActivity可能没有足够的信息,该ImageLoader的构造需求。

+0

明白了。它现在运作良好。谢谢你,先生! – Enovyne

相关问题