android - 通过从SharedPreference填充Textview使Android应用程序崩溃

标签 android crash textview sharedpreferences

我想用int形式sharedPreference填充textview。
因此,我在MainActivity(onCreate)中调用以下内容。

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

阅读计数器:
public int readCounter() {
        SharedPreferences pref = getSharedPreferences("ANZAHL", 0);
        return pref.getInt("COUNT", 0);
    }

现在我得到了这些错误:
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

我的MainActivity的第64行是:
counter.setText("Punkte: " + Integer.toString(readCounter()));  

主要:
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>

我做错了什么?

最佳答案

尝试将逻辑放在片段的onCreateView而不是 Activity 的onCreate中。

看看:Android:ActionBarActivity findViewById return NULL

关于android - 通过从SharedPreference填充Textview使Android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23801608/

相关文章:

java - Android:无法从静态上下文中引用非静态方法

android - 实例化一个ListAdapter//使用AsymmetricGridView时出错

android:ellipsize ="end"和 android:maxEms 不工作

android - 更改自定义对话框标题的字体

android - 从 Chromium for Android 构建 WebView

Java BeanMap Iterator 在 iterator.remove() 上抛出 UnsupportedOperationException

android - setContentView(R.layout.activity_main) 导致应用程序崩溃

c++ - 可以阻止单个应用程序的 Microsoft 错误报告吗?

android - 如何在android中的textview中设置运行时背景图像?

android - 如何检查 TextView 是否在 Visible Screen Android 中