(TextView)findbyid crashes application Android.
When i try to create a textview and link it to a XML component in my application my app seems to crash. the code i am using is:
public class WorkoutAdvice extends Activity {
TextView adviceshow;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String[] advice = getResources().getStringArray(R.array.races_array);
Random rand = new Random();
int ad = rand.nextInt(5);
String gen = advice[ad];
adviceshow = (TextView)findViewById(R.id.advice);
adviceshow.setText(gen);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.advice);
}
}
and the logcat for when my application closes is
05-26 01:43:23.018: E/AndroidRuntime(363): FATAL EXCEPTION: main 05-26 01:43:23.018: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutAdvice}: java.lang.NullPointerException 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread.main(Activi tyThread.java:4627) 05-26 01:43:23.018: E/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method) 05-26 01:43:23.018: E/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:521) 05-26 01:43:23.018: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-26 01:43:23.018: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-26 01:43:23.018: E/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method) 05-26 01:43:23.018: E/AndroidRuntime(363): Caused by: java.lang.NullPointerException 05-26 01:43:23.018: E/AndroidRuntime(363): at com.b00348312.workout.WorkoutAdvice.onCreate(WorkoutAdvice.java:22) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-26 01:43:23.018: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 05-26 01:43:23.018: E/AndroidRuntime(363): ... 11 more
the application works without the statements dealing with the findbyID lines which work in other parts of the application
Until you actually set your activity content by calling setContentView
, there are no views to be found. Once you have called setContentView
, then you may try to find your TextView
. Note that before this call, the Activity
has no knowledge at all of which(s) XML layouts will it be binded to (if any).
Put
setContentView(R.layout.advice);
before of
adviceshow = (TextView)findViewById(R.id.advice);
我想你应该使用findViewById之前setContentView。
链接地址: http://www.djcxy.com/p/31430.html