the application has stopped usw.

J

jmarple

Neues Mitglied
0
Hallo,

ich habe schon bei einigen themen mit demselben titel geguckt, und keine meldung war meiner ähnlich.
ich verstehe den log-cat nicht so richtig. ich habe alles so gemacht wie im buch, aber der emulator stoppt die app immer.

poste mal den log-cat:

06-21 13:44:15.727: E/AndroidRuntime(334): FATAL EXCEPTION: main
06-21 13:44:15.727: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.MainActivity}: java.lang.NullPointerException
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-21 13:44:15.727: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 13:44:15.727: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
06-21 13:44:15.727: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-21 13:44:15.727: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-21 13:44:15.727: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
06-21 13:44:15.727: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
06-21 13:44:15.727: E/AndroidRuntime(334): at com.example.helloandroid.MainActivity.onCreate(MainActivity.java:51)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 13:44:15.727: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-21 13:44:15.727: E/AndroidRuntime(334): ... 11 more

kann mir jemand weiterhelfen? vielen dank für eure hilfe.
 
Mhm - das hilft mir leider nicht weiter. Vielleicht hilft es, wenn ich die MainActivity mal poste?!?


Code:
package com.example.helloandroid;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {
	
	private static final int WILLKOMMEN_DIALOG = 1;
	private TextView textViewHalloAndroid;
	private EditText editTextVornameNachname;
	private Button buttontoast;
	private Button buttonSagHallo;
	private Button buttonwillkommen;
	private AnimationDrawable animatedSkater;
	private ImageView imageViewHalloAndroidKopf;
	
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.helloandroidlayout);
        
        textViewHalloAndroid = (TextView) this.findViewById(R.id.textViewhalloAndroid);
        editTextVornameNachname = (EditText) this.findViewById(R.id.editTextVornameNachname);
    	buttontoast = (Button) this.findViewById(R.id.buttontoast);
    	buttonSagHallo = (Button) this.findViewById(R.id.buttonSagHallo);
    	buttonwillkommen = (Button) this.findViewById(R.id.buttonwillkommen);
    	
    	buttontoast.setOnClickListener(this);
    	buttonSagHallo.setOnClickListener(this);
    	buttonwillkommen.setOnClickListener(this);

    	if (imageViewHalloAndroidKopf.getDrawable() instanceof AnimationDrawable){
    		
    		animatedSkater = (AnimationDrawable)this.imageViewHalloAndroidKopf.getDrawable();
    		Handler handler = new Handler(getMainLooper());
    		handler.postDelayed(new Runnable() {
    			
    			@Override    			
    			public void run(){
    			
    				animatedSkater.start();
    			}
    			
    		}
    		,1500);    		
    		
    	}
    	
    	
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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);
    }

    
   


	@Override
	public void onClick(View v) {
		
     switch (v.getId())
      {
      case R.id.buttonSagHallo:
    	  sagHallo();
    	  break;
    	  
      case R.id.buttontoast:
    	  ToastAussprechen();
    	  break;
    	  
      case R.id.buttonwillkommen:
    	  willkommen();
    	  break;	  
      
      }
	}


	private void willkommen() {
		this.showDialog(WILLKOMMEN_DIALOG);
		
		
		
	}


	private void sagHallo() {
		
		CharSequence vornameName = this.editTextVornameNachname.getText();	
		CharSequence sagHalloText = this.getResources().getString(R.string.saghallotext, vornameName);
		this.textViewHalloAndroid.setText(sagHalloText);
		
	}


	private void ToastAussprechen() {
		
		CharSequence vornameName = this.editTextVornameNachname.getText();	
		CharSequence toastText = this.getResources().getString(R.string.Toast);
		Toast.makeText(this, toastText+""+vornameName, Toast.LENGTH_LONG).show();
		
	}


	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		
		CharSequence vornameName = this.editTextVornameNachname.getText();	
		CharSequence sagHalloText = this.getResources().getString(R.string.saghallotext, vornameName);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(sagHalloText)
		.setTitle(R.string.hello_world)
		.setIcon(R.drawable.ic_launcher)
		.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				okGedrueckt(dialog);
				dialog.dismiss();
				
			}
		}).setNegativeButton(R.string.Abbruch, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				abbruchGedrueckt(dialog);
				dialog.dismiss();
			}
				
			});
		
		dialog = builder.create();
		
		return dialog;
	}
	
	private void okGedrueckt(DialogInterface dialog){
		
		Toast.makeText(this, "OK gedrückt", Toast.LENGTH_LONG).show();
		
	}
	
	private void abbruchGedrueckt(DialogInterface dialog){
		
		Toast.makeText(this, "Abbruch gedrückt", Toast.LENGTH_LONG).show();
		
	}

Ich weiß nicht, wo da der NullPointer sein soll. :confused2:
 
Hello,
here we go:
Code:
at com.example.helloandroid.MainActivity.onCreate(Mai  nActivity.[COLOR=Red]java:51[/COLOR])
Da es in der onCreate ist vermute ich, dass du in der MainAct. in Zeile 51 auf eine Ressource zugreifst, die es nicht gibt.

Was soll das machen?
Code:
if (imageViewHalloAndroidKopf.getDrawable() instanceof AnimationDrawable){
Sicher, dass das so geht?
http://stackoverflow.com/questions/16641376/set-imageview-to-a-drawable-android
(sicher bin ich mir allerdings auch nicht, ob da der Fehler herkommt :D bin gerade auf dem Sprung)
 
Zuletzt bearbeitet:
imageViewHalloAndroidKopf ist null. Du hast es nirgends initialisiert und versuchst dann getDrawable() aufzurufen.
 
jmarple schrieb:
Mhm - das hilft mir leider nicht weiter. Vielleicht hilft es, wenn ich die MainActivity mal poste?!?
Ich weiß nicht, wo da der NullPointer sein soll. :confused2:

So gesehen kann ich auch nur auf den Link verweisen, den Android_DEV bereits gepostet hat.
Falls du nochmal in Kürze erklärt haben willst was ein Nullpointer ist, hier mein Versuch:

Jedes Objekt, wie zB ein Textfeld des Typs TextView, ein Button des Typs Button, oder eine Instanz (beachte: Operator "new") einer eigenen Klasse braucht eine Referenz, um "ansprechbar" (Punkt-Operator: "." ) zu sein. Wenn man nun etwas ansprechen will was nicht "verlinkt" bzw. "bekannt" ist, gibt es eine Nullpointer-Exception.
Da für gewöhnlich viele Objekte bei der Android-Programmierung angesprochen werden, ist dieser Fehler keine Seltenheit. Allerdings ist es sehr wichtig diese Fehler finden und beheben zu können.
 
Zum "komischen" null:

null != 0

Wenn Du ein Objekt anlegst, wird Speicherplatz reserviert. Jedoch steht da noch nichts drin, also null. Erst durch eine Zuweisung erhält diese Objekt seinen "Sinn".

Ein ganz nettes Beispiel war der Vergleich mit Wohnorten.

1. Angenommen "Hans" wohnt in der Hauptstraße 4:
Code:
String wohnort_hans = "Hauptstraße 4"
Hans hat also eine genauen Wohnort, wo er öfter anzutreffen sein sollte.

2. Angenommen, Hans hat keinen Wohnort, weil er obdachlos ist:
Code:
String wohnort_hans = ""
Man weiß also, das Hans keinen Wohnort hat, und überall sein kann.

3. Wenn Hans aber jetzt den Wohnort = null hat:
Code:
String wohnort_hans = null

Dann ist Hans vlt. ein Agent, der nicht gefunden werden möchte. Er ist nirgendwo und überall. Also ist der Wohnort von Hans nicht genau definiert.
 

Ähnliche Themen

F
Antworten
1
Aufrufe
2.240
jogimuc
J
K
Antworten
1
Aufrufe
1.096
markus.tullius
markus.tullius
Zurück
Oben Unten