1

Before I go into detail about the troubles that I have been having, let me explain that I am pretty new to android development and I will explain what I am trying to achieve, where I am at so far and the problem that I have encountered.

I am developing an android app in version 4.0 using eclipse and what I would like to create is a login page with a username and password which is inputted by a user then checked by an external database to validate and display an alert saying whether the login is successful or not. If it is successful, the app will load a new page with an image obtained from a web source such as this: http://community.dur.ac.uk/cs.seg05/Wild/CameraImages/IMG_0364.JPG being displayed on the page. There will be some more information displayed around this page to help a user classify what is in the image and once the user is finished with this image, they would press a button (i have not put this in yet) and a new image would pop up and the process repeats.

So far, I have managed to get the login page working and I can load up a new page/activity fine with text and default widgets etc and using default images supplied by the android project. However, the problem comes when I am trying to access an image from a url and place it in an image viewer.

I have read and tried about 7-10 different implementations on how to do this (https://stackoverflow.com/questions/17495970/image-view-null-pointer-exception,https://stackoverflow.com/questions/4417552/null-pointer-exception-when-drawing-an-image, Null pointer exception when getting image from url to put in imageview, How to load an ImageView by URL in Android?, https://www.youtube.com/watch?v=KmYJBhz1gmk are just some examples ) but every single one I have tried returns the following error message in LogCat which then crashes the app, regardless of what url it is and what type the image is (jpg, png etc):

Nb com.example.hello is the package name (it was just what was there when i created the project)

Fatal Exception: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hello/com.example.hello.SecondActivity}: java.lang.NullPointerException
at android.app.Activity.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invokeNative(Method.java:511)
at com.android.internal.os.ZygotInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygotInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
(there is sometimes an extra error line here depending on what implementation I tried)
at com.example.hello.SecondActivity.onCreate(SecondActivity.java:58)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
... 11 more

Now I don't think that Internet permissions is a problem as i have had to do some internet access with some php files prior to encountering this problem and they all work and i also have the user permission in the android manifest xml file which you will see further down on this post. Additionally, I do not think its a url problem as i have tried the url's out in my browser and they are all fine.

So what I ask is this:

What is causing this problem?

What is the approach for tackling the problem (step by step would be useful) and the best implementation to use with what I have described that I want to achieve? (some code would be very helpful)

Do I need to post some more information on here?

Can anyone help

Thanks

P.S Here is the code that I have in my "SecondActivity" which works without the url stuff in (note that the getImages method works fine for something that I need for the future once this issue solved)

package com.example.hello;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.os.Build;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
public class SecondActivity extends Activity {
private String userId; //global variable as it will be used throughout this activity
private ArrayList <String> imageNames;
private int imageNumber=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Bundle data = getIntent().getExtras(); //obtains information passed from the previous activity
    userId= data.getString("userId"); //gets the user id from the main activity
    System.err.println("Received User  id " +userId);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    imageNames =getImages(userId); //gets all of the image names from the database
    System.err.println(imageNames);

    ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_second,
                container, false);
        return rootView;
    }
}


public ArrayList<String> getImages(String userid) {
    ArrayList<String> images= new ArrayList<String>();
    try {
        HttpClient httpclient= new DefaultHttpClient();
        URI website= new URI("https://community.dur.ac.uk/cs.seg05/androidapp/ImageAccess.php?userid="+userid);
        HttpGet request= new HttpGet();
        request.setURI(website);

        HttpResponse response = httpclient.execute(request);

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb=new StringBuffer("");
        String l= "";
        String nl= System.getProperty("line.separator");

        while ((l = reader.readLine()) != null) { //converts the whole output into a string 
            sb.append(l+ nl);
    }
        reader.close();
        String result=sb.toString();
        //System.err.println(result);





            JSONArray jArray = new JSONArray(result);
            System.err.println(jArray);
            for(int i=0;i<jArray.length();i++){ //retrieves the JSon
                    JSONObject json_data = jArray.getJSONObject(i);
                    images.add(json_data.getString("File_Name"));



    }


        }
        catch (Exception e) {

        }


    return images;


}

}

Here is the xml with imageview contained that the activity is mapped to:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hello.SecondActivity$PlaceholderFragment" >

  <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="144dp" />

</RelativeLayout>

Here is the xml for my Android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="14" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.hello.MainActivity" //login activity
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.hello.SecondActivity"
        android:label="@string/app_name" >
    </activity>
</application>

Hope that this is enough information. If not, let me know and I will post it

Here is an example of what i have tried

Bitmap bm= getBitmapFromURL("http://community.dur.ac.uk/cs.seg05/Wild/CameraImages/1");
    ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
    iv.setImageBitmap(bm); //this is line 58 in this example 

where the getBitmapFromURL is this method

public static Bitmap getBitmapFromURL(String src) {
        try {
            java.net.URL url = new java.net.URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
Community
  • 1
  • 1
bardsleyta
  • 151
  • 1
  • 2
  • 14

2 Answers2

0

This

at com.example.hello.SecondActivity.onCreate(SecondActivity.java:58)

Says that the error is on line 58 of SecondActivity.java. Line 58 is here:

ImageView iv= (ImageView)findViewById(R.id.imageView1); //gets the image view
iv.setImageBitmap(bm); //this is line 58 in this example 

So, looks like iv is null

iv is null because activity_second.xml does not contain an ImageView with the id imageView1

To fix, you can either move all your code into your Fragment class, or move the xml into your activity_main.xml

It's probably also worth looking again into the Fragment documentation http://developer.android.com/guide/components/fragments.html

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Ok thanks. Have changed that xml file to include it and it now works :). to clarify ,in order to change this image that is displayed, would you then make another url request and do the same thing? – bardsleyta Apr 11 '14 at 19:40
  • Yes you can just call `getBitmapFromURL` again and `iv.setImageBitmap(bm)` again. – Ken Wolf Apr 11 '14 at 19:41
  • ok thank you so much for your help. i was stuck on this problem for 2 days and am glad that you have helped me find the solution – bardsleyta Apr 11 '14 at 19:42
  • do you have to reset the imageview when you want to change the image when a button is clicked? – bardsleyta Apr 12 '14 at 19:02
  • calling `setImageBitmap(bm)` again should be all you need to change the image, I'm not sure what you mean by reset. There's no need to initialise it again via `findViewById` if that's what you mean. – Ken Wolf Apr 12 '14 at 20:28
0

My answer is not related to your problem which is solved.

But you might want to have a look at AsyncTask to perform your image downloads instead of doing them within the UI thread.

http://developer.android.com/reference/android/os/AsyncTask.html

There is also an interesting post on android developer blog :

http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

Kevin LE GOFF
  • 773
  • 8
  • 25