Bildauswahl aus der Galery führt zum Fehler NullObjectReference

  • Antworten:10
Hanno Wallah
  • Forum-Beiträge: 49

17.04.2016, 14:17:01 via Website

Mittels Intent (startActivityFor Result) möchte ich ein Bild aus der Galery auswählen. Die Methode ist in eigener Klasse impelemtiert und beim Aufruf kommt es zum NullPointerException. Wenn ich aber starActivity aufrufe, dann werden die Bilder aus der Gaalery angezeigt. Ich benötige aber nach der Auswahl das Bild (mittels onActivityResult).
Der Code sieht wie folgt aus:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
//ctx.startActivity(Intent.createChooser(intent, "SelectImage")); dies funktioniert

    startActivityForResult(Intent.createChooser(intent, "SelectImage"), 1);    dies führt zum Fehler

}catch (Exception ex){
    Toast.makeText(ctx,ex.toString(),Toast.LENGTH_LONG).show();
}

Folgende Fehlermeldung wird ausgegeben;

Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference ????????????

Wo ist der Fehler????

— geändert am 17.04.2016, 14:19:35

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

17.04.2016, 14:18:50 via Website

Meine Glaskugel ist ausgefallen :P;)

Vielleicht willst du mal deinen Cde zeigen, dann kann man mehr sagen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Hanno Wallah
  • Forum-Beiträge: 49

17.04.2016, 14:20:21 via Website

public class IntentForGalary extends Activity{
ImageView _imView = null;

TextView nameView;

public void getImage(Context ctx, ImageView imView) {

_imView = imView;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
  //ctx.startActivity(Intent.createChooser(intent, "SelectImage"));

    startActivityForResult(Intent.createChooser(intent, "SelectImage"), 1);

}catch (Exception ex){
    Toast.makeText(ctx,ex.toString(),Toast.LENGTH_LONG).show();
}

}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data){
if (resCode == RESULT_OK){
if (reqCode == 1){
Uri imageUri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
            // Log.d(TAG, String.valueOf(bitmap));

            //ImageView imageView = (ImageView) findViewById(R.id.imageView);
            _imView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }
}

}

— geändert am 17.04.2016, 14:30:25

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

17.04.2016, 14:53:12 via Website

Du erstellst da intent falsch.
So ist es besser:

  Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(pickPhoto, 1);//one can be replaced with any action code

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Hanno Wallah
  • Forum-Beiträge: 49

17.04.2016, 15:08:03 via Website

Habe wie folgt probiert:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

Leider kommt die selbe FM.

Ich habe auch mi diversen Context übergaben probiert; auch ohne Erfolg.
Scheint eine harte Nuss zu sein?

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

17.04.2016, 15:42:08 via App

Hast du mein Code Probiert??

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Hanno Wallah
  • Forum-Beiträge: 49

17.04.2016, 15:59:03 via Website

Ja; Leider derselbe Effekt. Habe meine Zeilen ausgesternt und die von dir empfohlenen übernommen.

Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference.

Ferner: in der MainActivity funktioniert es. Leider kann ich es dort nicht gebrauchen.

— geändert am 17.04.2016, 18:42:50

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

17.04.2016, 18:51:32 via App

Startest du die 2. Activity auch per Start activity oder machst du einfach eine neue Instanz?
Zeig mal mehr Code und den Detalierten Fehler

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Hanno Wallah
  • Forum-Beiträge: 49

18.04.2016, 12:53:05 via Website

Sorry es hat länger gedauert.

Ich habe eine CursorAdapter Klasse, aus dem ich die Methode (getImage) der Klasse IntentForGalary (extends Activity) starte.
Der aufruf aus der Adapter Klasse ist wie folgt:

_ivFavorite_icon = (ImageView) view.findViewById(R.id.favorite_icon);

IntentForGalary ifg = new IntentForGalary();
ifg.getImage(this.context, _ivFavorite_icon);

Der ganze Fehlercode:

04-18 10:48:37.360 2393-2393/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hannowallah.mydictionaryswipe, PID: 2393
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at com.example.hannowallah.mydictionaryswipe.IntentForGalary.getImage(IntentForGalary.java:36)
at com.example.hannowallah.mydictionaryswipe.SwipeViewAdapter.onClick(SwipeViewAdapter.java:200)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

18.04.2016, 13:07:05 via Website

Das wird so nicht funktionieren, eigentlich musst du es aus deiner MainActivity machen...

Dein Adapter darf keine Activity sein, nimm das Extends da wieder raus.

Dnn wird er sich beschweren dass es onActivityResult nicht gibt bzw. nicht überschreiben werden kann.
Da nimmst du einfach das @Override weg.

Sobald das erledigt ist kannst du deinen Adapter benutzen:

Activity:

IntentForGalary ifg; //Klassenvar

myMethod()
{
_ivFavorite_icon = (ImageView) view.findViewById(R.id.favorite_icon);

ifg = new IntentForGalary();
ifg.getImage(this.context, _ivFavorite_icon);
}


Und deine MainActivity muss auch ein Override auf die ActivityResult haben:

@Override
public void onActivityResult(int reqCode, int resCode, Intent data){ 
{
ifg.onActivityResult(reqCode,resCode,data); // Adapter handeln lassen
// evntl vom Adpter dann ein Bitmap oder Drawable zurückgeben und das dann auf eine ImageView setzen
}

Du kannst das ganze nicht in einer Methode machen, du musst über das Callback onActivityResult deiner aktiven Activity gehen, somit musst du dort dann das Bild laden und anzeigen oder onst was damit machen.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Hanno Wallah
  • Forum-Beiträge: 49

18.04.2016, 19:50:15 via Website

Herzlichen Dank für die Unterstützung. Es klappt wunderbat.

Pascal P.

Antworten