我正在编写一个Android应用程序,但它不起作用。它会立即打开和停止。我真的不知道问题出在哪里以及为什么会发生。
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.rec);
MyAdapter myAdapter=new MyAdapter(this,putName());
recyclerView.setAdapter(myAdapter);
LinearLayoutManager linearLayoutManager = new
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
}
public ArrayList<NameList> putName(){
ArrayList<NameList> chart=new ArrayList<>();
chart.add(new NameList("second one"));
chart.add(new NameList("third one"));
return chart;
}
}
我有一个SharedPrefences
课程:
public class MyShared extends Activity {
private SharedPreferences sharedPreferences=getSharedPreferences("pref",
Context.MODE_PRIVATE);
public void Shared(int Int,Boolean boolea){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean(Integer.toString(Int),boolea);
editor.apply();
}
public Boolean getShared(int Int){
Boolean boo=sharedPreferences.getBoolean(Integer.toString(Int),false);
return boo;
}
}
这是我的Namelist
:
public class NameList{
private String name;
public NameList(String name){
this.name=name;
}
public String getName() {
return name;
}
}
我有一个RecyclerView
和两个按钮:一个用于在我的MyShared
类中保存一个布尔值,另一个只是我的回收器的一个按钮。问题在哪里?
logcat的:
02-06 04:02:49.591: W/System(3573): ClassLoader referenced unknown path: /data/app/com.example.mehrad.first-2/lib/x86
02-06 04:02:49.679: W/art(3573): Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-06 04:02:49.990: D/AndroidRuntime(3573): Shutting down VM
02-06 04:02:49.991: E/AndroidRuntime(3573): FATAL EXCEPTION: main
02-06 04:02:49.991: E/AndroidRuntime(3573): Process: com.example.mehrad.first, PID: 3573
02-06 04:02:49.991: E/AndroidRuntime(3573): java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.getBoolean(java.lang.String, boolean)' on a null object reference
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.example.mehrad.first.MyShared.getShared(MyShared.java:20)
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.example.mehrad.first.MyAdapter.onBindViewHolder(MyAdapter.java:38)
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.example.mehrad.first.MyAdapter.onBindViewHolder(MyAdapter.java:19)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6310)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6343)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5289)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5552)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5394)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5390)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2149)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:593)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3537)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2979)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:383)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:431)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.View.measure(View.java:19857)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.Choreographer.doCallbacks(Choreographer.java:686)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.Choreographer.doFrame(Choreographer.java:621)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.os.Handler.handleCallback(Handler.java:751)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.os.Handler.dispatchMessage(Handler.java:95)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.os.Looper.loop(Looper.java:154)
02-06 04:02:49.991: E/AndroidRuntime(3573): at android.app.ActivityThread.main(ActivityThread.java:6119)
02-06 04:02:49.991: E/AndroidRuntime(3573): at java.lang.reflect.Method.invoke(Native Method)
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
02-06 04:02:49.991: E/AndroidRuntime(3573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我编辑了我的logcat
答案 0 :(得分:3)
您在创建活动之前调用了getSharedPreferences()。在onCreate()方法中进行调用。
这样的事情:
public class MyShared extends Activity {
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
}
...
}