我在TextView
启动时尝试更改MainActivity
的字体大小,但在这种情况下我遇到NullPointerException
错误!
这是日志:
08-10 05:05:51.821 2363-2363/com.twitter.i_droidi.notah E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.twitter.i_droidi.notah, PID: 2363
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twitter.i_droidi.notah/com.twitter.i_droidi.notah.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.twitter.i_droidi.notah.NotesAdapter$NotesViewHolder.noteTitle' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.twitter.i_droidi.notah.NotesAdapter$NotesViewHolder.noteTitle' on a null object reference
at com.twitter.i_droidi.notah.MainActivity.onStart(MainActivity.java:153)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private NotesAdapter.NotesViewHolder myHolder;
private SharedPreferences fontSizePrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
fontSizePrefs = getSharedPreferences(Preferences.PREFS_KEY, Context.MODE_PRIVATE);
...
...
}
@Override
protected void onStart() {
super.onStart();
switch(fontSizePrefs.getInt("font_size", -1))
{
case 0:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) 19.5);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) 12.75);
return;
case 1:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
return;
case 2:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
return;
case 3:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return;
case 4:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
return;
case 5:
myHolder.noteTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
myHolder.noteBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
myHolder.noteDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
return;
}
}
NotesAdapter.java:
public class NotesAdapter extends RealmRecyclerViewAdapter<Notes> {
private final Context context;
private Realm realm;
protected NotesAdapter(Context context)
{
this.context = context;
}
@Override
public NotesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
return new NotesViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
realm = RealmController.getInstance().getRealm();
final Notes note = getItem(position);
final NotesViewHolder myHolder = (NotesViewHolder) holder;
myHolder.noteTitle.setText(note.getTitle());
myHolder.noteBody.setText(note.getBody());
myHolder.noteDate.setText(note.getDate());
myHolder.card.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final RealmResults<Notes> results = realm.where(Notes.class).findAll();
Notes note = results.get(position);
final String title = note.getTitle();
AlertDialog.Builder deleteDialog = new AlertDialog.Builder(context);
deleteDialog.setTitle(R.string.delete_a_note_title);
deleteDialog.setMessage(context.getResources().getString(R.string.delete_a_note_msg) + title + context.getResources().getString(R.string.delete_a_note_msg2));
deleteDialog.setIcon(R.drawable.delete);
deleteDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
realm.beginTransaction();
results.deleteFromRealm(position);
realm.commitTransaction();
notifyDataSetChanged();
MainActivity obj = new MainActivity();
obj.updateView();
Toast.makeText(context, R.string.note_deleted, Toast.LENGTH_SHORT).show();
}
});
deleteDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// DO NOT DO ANYTHING.
}
});
deleteDialog.show();
return false;
}
});
myHolder.card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ViewNote.class);
intent.putExtra("id", position);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
if(getRealmAdapter() != null)
{
return getRealmAdapter().getCount();
}
return 0;
}
protected static class NotesViewHolder extends RecyclerView.ViewHolder
{
private CardView card;
protected TextView noteTitle;
protected TextView noteBody;
protected TextView noteDate;
protected NotesViewHolder(View view)
{
super(view);
card = (CardView) view.findViewById(R.id.card_view);
noteTitle = (TextView) view.findViewById(R.id.note_title);
noteBody = (TextView) view.findViewById(R.id.note_body);
noteDate = (TextView) view.findViewById(R.id.note_date);
}
}
}
card_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"
android:paddingTop="2dp"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:orientation="vertical"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp"
card_view:contentPadding="8dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/relativelayout_of_cards"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="19.5sp"
android:textColor="#212121"
android:layout_marginTop="8dp"
android:textStyle="bold"
android:typeface="serif"
android:fontFamily="serif" />
<TextView
android:id="@+id/note_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textSize="15sp"
android:textColor="#727272"
android:autoLink="all"
android:linksClickable="false"
android:layout_below="@+id/note_title"
android:maxLines="3"
android:typeface="serif"
android:fontFamily="serif" />
<TextView
android:id="@+id/note_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12.75sp"
android:typeface="serif"
android:fontFamily="serif"
android:textStyle="bold"
android:layout_below="@+id/note_body"
android:paddingTop="12dp"
android:textColor="#212121"
android:layout_marginBottom="2dp"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="false"
android:textAllCaps="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>