-1

I want to fill a textview with an int form sharedPreference. So I call the following in my MainActivity (onCreate).

TextView counter = (TextView) findViewById(R.id.punkte);
counter.setText("Punkte: " + Integer.toString(readCounter()));

read Counter:

public int readCounter() {
        SharedPreferences pref = getSharedPreferences("ANZAHL", 0);
        return pref.getInt("COUNT", 0);
    }

Now I got these errors:

05-22 08:20:44.215: E/AndroidRuntime(1269): FATAL EXCEPTION: main
05-22 08:20:44.215: E/AndroidRuntime(1269): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.basti12354.bikinifigur.lite/de.basti12354.bikinifigur2.MainActivity}: java.lang.NullPointerException
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.os.Looper.loop(Looper.java:137)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at java.lang.reflect.Method.invoke(Method.java:525)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at dalvik.system.NativeStart.main(Native Method)
05-22 08:20:44.215: E/AndroidRuntime(1269): Caused by: java.lang.NullPointerException
05-22 08:20:44.215: E/AndroidRuntime(1269):     at de.basti12354.bikinifigur2.MainActivity.onCreate(MainActivity.java:64)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.Activity.performCreate(Activity.java:5133)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-22 08:20:44.215: E/AndroidRuntime(1269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-22 08:20:44.215: E/AndroidRuntime(1269):     ... 11 more

Line 64 of my MainActivity is:

counter.setText("Punkte: " + Integer.toString(readCounter()));  

MAIN:

public class MainActivity extends ActionBarActivity implements OnClickListener {

    public ImageView einstellungen;
    public ImageView information;
    public ImageView uebung;
    public ImageView start;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayShowHomeEnabled(false);


        //Lautstärke BUttons enable
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        //OnClickListener
        einstellungen = (ImageView) findViewById(R.id.anleitung);
        einstellungen.setOnClickListener(this);

        information = (ImageView) findViewById(R.id.impressum);
        information.setOnClickListener(this);

        uebung = (ImageView) findViewById(R.id.uebungen);
        uebung.setOnClickListener(this);

        start = (ImageView) findViewById(R.id.start);
        start.setOnClickListener(this);




        //Punkte
        //Counter count = new Counter();
        TextView counter = (TextView) findViewById(R.id.punkte);
        counter.setText("Punkte: " + Integer.toString(readCounter()));      



    }


    /**
     * 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, container,
                    false);
            return rootView;
        }
    }
    public void verlaufKlick(View view) {  
        Intent intent = new Intent(view.getContext(), Einstellungen.class);
        startActivity(intent);



    }// calling next site, that means activity2

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.uebungen:
            Intent intent = new Intent(v.getContext(), Uebungen.class);
            startActivity(intent);
            break;

        case R.id.anleitung:
//          Intent intent2 = new Intent(v.getContext(), Einstellungen.class);
//          startActivity(intent2);
            Intent intent2 = new Intent(Intent.ACTION_VIEW);
            intent2.setData(Uri.parse("market://details?id=de.basti12354.bikinifigur2"));
            startActivity(intent2);
            break;

        case R.id.impressum:
            Intent intent3 = new Intent(v.getContext(), Information.class);
            startActivity(intent3);
            break;

        case R.id.start:
            Intent intent1 = new Intent(v.getContext(), Start.class);
            startActivity(intent1);
            break;

        }

    }

    public int readCounter() {
        SharedPreferences pref = getSharedPreferences("ANZAHL", 0);
        return pref.getInt("COUNT", 0);
    }



}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
   >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bannerblau"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:layout_above="@+id/start"
        android:layout_centerHorizontal="true" />

     <ImageButton
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_above="@+id/zwei"
        android:background="?android:selectableItemBackground"
        android:layout_centerHorizontal="true"
        android:src="@drawable/start"

        />
<LinearLayout 
    android:layout_centerHorizontal="true"
    android:id="@+id/zwei"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/impressum">

<ImageButton
        android:id="@+id/uebungen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        android:background="?android:selectableItemBackground"

        android:src="@drawable/uebungen"
        />
<ImageButton
        android:id="@+id/anleitung"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        android:background="?android:selectableItemBackground"

        android:src="@drawable/anleitung"
        />

   </LinearLayout>

     <ImageButton
        android:id="@+id/impressum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        android:background="?android:selectableItemBackground"
        android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
        android:src="@drawable/impressum"
        />

     <TextView
         android:id="@+id/punkte"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/imageView1"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="54dp"
          />

</RelativeLayout>

What I am doing wrong?

basti12354
  • 2,490
  • 4
  • 24
  • 43

1 Answers1

1

Try to put your logic inside fragment's onCreateView instead of activity's onCreate.

Take a look at: Android:ActionBarActivity findViewById return NULL

Community
  • 1
  • 1
Zoran
  • 1,484
  • 1
  • 10
  • 13
  • A few minutes ago, I created a new project. I only create one textview and doing the same in the MainActivity; findById -> setText IT WORKS FINE, BUT I DONT KNOW WHY – basti12354 May 22 '14 at 21:36
  • Did you use fragment or not? If yes, did you put findById -> setText inside onCreateView of fragment? – Zoran May 23 '14 at 00:01
  • No I only use one layout, without fragments. I put my findById to my onCreate – basti12354 May 23 '14 at 09:14
  • Well, that's the reason. In your example you used PlaceholderFragment and your findById -> setText was in onCreate (and it should be in onCreateView). That caused errors. Now you don't have fragment so you can put findById -> setText in onCreate. – Zoran May 23 '14 at 09:32
  • Did this help you or you still have problem? If it works than please aprove it as solved. – Zoran May 23 '14 at 12:51
  • No, I delete PlaceHolderFragment now, but same error by calling counter.setText("Hi"); – basti12354 May 23 '14 at 15:09