Java 文件返回 NullPointerException
Java file returning NullPointerException
我正在尝试制作一个简单的 activity 来显示用户的高分。它不是一个列表,它只是一个高分。每当我在 mainActivity 中单击 "See your records" 时,应用程序就会崩溃,并且 logcat 会这样说:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access0(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1914)
at haveabeer.two.padc.haveabeer.Records.<init>(Records.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2220)
中途看的很清楚,错误发生在Records.java18,问题是这样的,Java18是一个textView。几个小时以来,我一直在努力解决这个问题,但一无所获。这是 Class
Records.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class Records extends MainActivity {
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
public int default_int_day = 0;
public int default_int_week = 0;
static boolean daily_recieved = false;
static boolean weekly_recieved = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal_records);
SharedPreferences sharedPrefs = getSharedPreferences("Data", Context.MODE_PRIVATE);
int Daily_Record_Option = sharedPrefs.getInt("daily", counter);
int Weekly_Record_Option = sharedPrefs.getInt("weekly", counter_weekly);
if(isSavedDaily){
if(Daily_Record_Option > default_int_day){
daily_record.setText(Daily_Record_Option);
default_int_day = Daily_Record_Option;
daily_recieved = true;
}else if(default_int_day > Daily_Record_Option){
daily_record.setText(default_int_day);
daily_recieved = false;
}else{
daily_record.setText("No records yet!");
}
}else{
Toast.makeText(this, "No daily data saved yet", Toast.LENGTH_LONG).show();
}
if(isSavedWeekly){
if(Weekly_Record_Option > default_int_week){
weekly_record.setText(Weekly_Record_Option);
default_int_week = Weekly_Record_Option;
weekly_recieved = true;
} else if(default_int_week > Weekly_Record_Option){
weekly_record.setText(default_int_week);
weekly_recieved = false;
}
}else{
Toast.makeText(this, "No weekly data saved yet", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.records_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.Back){
Intent records_intent = new Intent(this, MainActivity.class);
startActivity(records_intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
以及附加的布局文件:
personal_records.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:background="#FF8B00"
android:id="@+id/Personal_Records">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Your_Records"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="40dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Weekly_Record"
android:id="@+id/textView6"
android:layout_below="@+id/textView5"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/weekly_textview"
android:layout_below="@+id/textView6"
android:layout_alignLeft="@+id/textView6"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignStart="@+id/textView6"
android:gravity="center_horizontal"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Daily_Record"
android:id="@+id/textView7"
android:layout_below="@+id/weekly_textview"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5"
android:layout_marginBottom="20dp"
android:paddingTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/daily_textview"
android:layout_below="@+id/textView7"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignLeft="@+id/textView6"
android:layout_alignStart="@+id/textView6"
android:text=" "
android:gravity="center_horizontal"/>
</RelativeLayout>
我不确定 textView 怎么会 return 为 null,特别是考虑到它应该位于常规 activity 而不是片段中。感谢所有帮助!
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
您想在 onCreate
方法中初始化您的 TextView,在 setContentView
之后。在调用 onCreate
和 setContentView
之前,您需要查找视图的内容不会被初始化。
你需要在setContentView后的onCreate方法中初始化两个textview。
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
在 setContentView(R.layout.personal_records) 之后初始化视图;因为Activity没有载入XML文件,所以找不到TextView
首先你必须经历 Activity 生命周期然后你只需要做 android program.In android 生命周期是重要的 one.All 你的 UI
渲染发生在 onCreate
only.So 中,你必须在 onCreate(Widget
初始化) 中初始化必须的值。你的状态实例由 [=14= 处理].
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
我正在尝试制作一个简单的 activity 来显示用户的高分。它不是一个列表,它只是一个高分。每当我在 mainActivity 中单击 "See your records" 时,应用程序就会崩溃,并且 logcat 会这样说:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access0(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1914)
at haveabeer.two.padc.haveabeer.Records.<init>(Records.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2220)
中途看的很清楚,错误发生在Records.java18,问题是这样的,Java18是一个textView。几个小时以来,我一直在努力解决这个问题,但一无所获。这是 Class Records.java:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class Records extends MainActivity {
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
public int default_int_day = 0;
public int default_int_week = 0;
static boolean daily_recieved = false;
static boolean weekly_recieved = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal_records);
SharedPreferences sharedPrefs = getSharedPreferences("Data", Context.MODE_PRIVATE);
int Daily_Record_Option = sharedPrefs.getInt("daily", counter);
int Weekly_Record_Option = sharedPrefs.getInt("weekly", counter_weekly);
if(isSavedDaily){
if(Daily_Record_Option > default_int_day){
daily_record.setText(Daily_Record_Option);
default_int_day = Daily_Record_Option;
daily_recieved = true;
}else if(default_int_day > Daily_Record_Option){
daily_record.setText(default_int_day);
daily_recieved = false;
}else{
daily_record.setText("No records yet!");
}
}else{
Toast.makeText(this, "No daily data saved yet", Toast.LENGTH_LONG).show();
}
if(isSavedWeekly){
if(Weekly_Record_Option > default_int_week){
weekly_record.setText(Weekly_Record_Option);
default_int_week = Weekly_Record_Option;
weekly_recieved = true;
} else if(default_int_week > Weekly_Record_Option){
weekly_record.setText(default_int_week);
weekly_recieved = false;
}
}else{
Toast.makeText(this, "No weekly data saved yet", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.records_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.Back){
Intent records_intent = new Intent(this, MainActivity.class);
startActivity(records_intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
以及附加的布局文件: personal_records.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:background="#FF8B00"
android:id="@+id/Personal_Records">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Your_Records"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="40dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Weekly_Record"
android:id="@+id/textView6"
android:layout_below="@+id/textView5"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/weekly_textview"
android:layout_below="@+id/textView6"
android:layout_alignLeft="@+id/textView6"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignStart="@+id/textView6"
android:gravity="center_horizontal"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Daily_Record"
android:id="@+id/textView7"
android:layout_below="@+id/weekly_textview"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5"
android:layout_marginBottom="20dp"
android:paddingTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/daily_textview"
android:layout_below="@+id/textView7"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignLeft="@+id/textView6"
android:layout_alignStart="@+id/textView6"
android:text=" "
android:gravity="center_horizontal"/>
</RelativeLayout>
我不确定 textView 怎么会 return 为 null,特别是考虑到它应该位于常规 activity 而不是片段中。感谢所有帮助!
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
您想在 onCreate
方法中初始化您的 TextView,在 setContentView
之后。在调用 onCreate
和 setContentView
之前,您需要查找视图的内容不会被初始化。
你需要在setContentView后的onCreate方法中初始化两个textview。
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);
在 setContentView(R.layout.personal_records) 之后初始化视图;因为Activity没有载入XML文件,所以找不到TextView
首先你必须经历 Activity 生命周期然后你只需要做 android program.In android 生命周期是重要的 one.All 你的 UI
渲染发生在 onCreate
only.So 中,你必须在 onCreate(Widget
初始化) 中初始化必须的值。你的状态实例由 [=14= 处理].
TextView daily_record = (TextView) findViewById(R.id.daily_textview);
TextView weekly_record = (TextView) findViewById(R.id.weekly_textview);