Hallo liebe Community,
an diesem schönen Sonntag wollte ich mich ein wenig mehr an das Programmieren wagen und habe ein wenig gebastelt ![]()
Leider bekomme ich beim ausführen der App eine Execption und die Anwendung beendet sich ohne je offen gewesen zu sein.
Da meine Kenntnisse sich noch im Aufbau befinden, finde ich leider meine(n) Fehler nicht. Vielleicht kann mir hier ja jemand helfen ![]()
Das ist der Quelltext der MainActivity
Java
		
					
			package com.example.felixhoevel.spielereien;
import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
    TextView test = (TextView) findViewById(R.id.textView_test);
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner_selection);        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.db_selection, android.R.layout.simple_spinner_item);        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        spinner.setAdapter(adapter);           }
    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.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();
        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }
        return super.onOptionsItemSelected(item);    }
    @Override    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {        parent.getItemIdAtPosition(position);
        if (position == 1) {            test.setText("1 wurde ausgewählt");        }        else            test.setText("0 ist default");    }
    @Override    public void onNothingSelected(AdapterView<?> parent) {
    }}
	
			Alles anzeigen
	Hier die Exception:
Code
		
			03-08 17:44:32.867  31944-31944/com.example.felixhoevel.spielereien E/AndroidRuntime﹕ FATAL EXCEPTION: main    Process: com.example.felixhoevel.spielereien, PID: 31944    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.felixhoevel.spielereien/com.example.felixhoevel.spielereien.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:155)            at android.app.ActivityThread.main(ActivityThread.java:5696)            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:1028)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference            at android.app.Activity.findViewById(Activity.java:2083)            at com.example.felixhoevel.spielereien.MainActivity.<init>(MainActivity.java:16)            at java.lang.reflect.Constructor.newInstance(Native Method)            at java.lang.Class.newInstance(Class.java:1572)            at android.app.Instrumentation.newActivity(Instrumentation.java:1083)            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)            at android.app.ActivityThread.access$800(ActivityThread.java:144)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:155)            at android.app.ActivityThread.main(ActivityThread.java:5696)            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:1028)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
	
	Das ganze wird auf meinem HTC One getestet mit Android 5
Danke im Voraus und einen schönen Sonntag euch noch ![]()
Felix