我想用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()));
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>
我做错了什么?
答案 0 :(得分:1)
尝试将您的逻辑放在片段的onCreateView而不是activity的onCreate中。