Unable to start activity ComponentInfo{com.example.hellotest/com.example.hellotest.TypefaceActivity}

09-24 11:28:04.471: E/AndroidRuntime(26563): FATAL EXCEPTION: main
09-24 11:28:04.471: E/AndroidRuntime(26563): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellotest/com.example.hellotest.TypefaceActivity}: java.lang.IllegalArgumentException: generateDefaultLayoutParams() cannot return null
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1957)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1982)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread.access$600(ActivityThread.java:124)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.os.Looper.loop(Looper.java:137)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread.main(ActivityThread.java:4456)
09-24 11:28:04.471: E/AndroidRuntime(26563): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 11:28:04.471: E/AndroidRuntime(26563): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 11:28:04.471: E/AndroidRuntime(26563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-24 11:28:04.471: E/AndroidRuntime(26563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-24 11:28:04.471: E/AndroidRuntime(26563): at dalvik.system.NativeStart.main(Native Method)
09-24 11:28:04.471: E/AndroidRuntime(26563): Caused by: java.lang.IllegalArgumentException: generateDefaultLayoutParams() cannot return null
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.view.ViewGroup.addView(ViewGroup.java:3162)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.view.ViewGroup.addView(ViewGroup.java:3145)
09-24 11:28:04.471: E/AndroidRuntime(26563): at com.example.hellotest.TypefaceActivity.onCreate(TypefaceActivity.java:35)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.Activity.performCreate(Activity.java:4465)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
09-24 11:28:04.471: E/AndroidRuntime(26563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1921)

09-24 11:28:04.471: E/AndroidRuntime(26563): ... 11 more


代码如下:

package com.example.hellotest;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


public class TypefaceActivity extends Activity{


private LinearLayout layout;
private LinearLayout.LayoutParams layoutParams;
private int WRAP_CONTENT = LinearLayout.LayoutParams.WRAP_CONTENT;
private TextView textView_style;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

layout = new LinearLayout(this);
// layout.setBackgroundResource(R.drawable.qq1);
layout.setOrientation(WRAP_CONTENT);
layout.setBackgroundColor(Color.GREEN);
setContentView(layout);

layoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);

textView_style = new TextView(this);
textView_style.setText("style");
textView_style.setTextColor(Color.BLUE);
textView_style.setTextSize(24.0f);//设置绘制的文本大小,值为浮点类型
textView_style.setTypeface(null, Typeface.BOLD);//设置字体风格
layout.addView(textView_style);

}
}

原因:获取主屏布局参数为空

修改:将layout.setOrientation(WEAP_CONTENT)改成layout.setOrientation(LinearLayout.VERTICAL).

你可能感兴趣的:(android)