1

我正在尝试在 android 应用程序中以编程方式添加 SeekBar。

这是我的代码:

SeekBar seekbar = new SeekBar(this);
    seekbar.setMax(6);
    seekbar.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.linearLayout1);
    linearLayout.addView(seekBar);

但是,当调用 addView 方法时,我得到了这个异常

06-21 22:11:26.286: E/AndroidRuntime(13354): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.T4J_OAuth.activities/com.T4J_OAuth.activities.ExampleTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.T4J_OAuth.activities/com.T4J_OAuth.activities.feedbackActivity}: java.lang.NullPointerException

我是不是对代码做错了什么。请注意,我尝试添加 seekBar 的活动是包含选项卡的父活动的一部分。因此,此活动是选项卡之一。

这是我的反馈活动类

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

    setContentView(R.layout.feedback_form)

    String unitName = "Unit:" + ExampleTabActivity.unitName;
    final TextView textViewToChange = (TextView) findViewById(R.id.textView1);
    textViewToChange.setText(unitName);

    String lectureName = "Lecture:" + ExampleTabActivity.lectureName;
    final TextView textViewToChange2 = (TextView) findViewById(R.id.textView2);
    textViewToChange2.setText(lectureName);



    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ExampleTabActivity.subsections);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    SeekBar seekbar = new SeekBar(this);
    seekbar.setMax(6);
    seekbar.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));*/
    LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.linearLayout1);
    linearLayout.addView(seekBar);
}

创建选项卡的 TabActivity 的这一部分

TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;

    Intent newIntent = new Intent().setClass(this, feedbackActivity.class);

    spec = tabHost
            .newTabSpec("structured")
            .setIndicator(createTabView(this, "Structured"))
            .setContent(newIntent);

    tabHost.addTab(spec);

    Intent newIntent2 = new Intent().setClass(this, feedbackNLActivity.class);

    spec = tabHost
            .newTabSpec("nl")
            .setIndicator(createTabView(this, "Natural Language"))
            .setContent(newIntent2);

    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab, null);
    TextView tv = (TextView) view.findViewById(R.id.tabText);
    tv.setText(text);
    return view;
}

这是错误的完整堆栈跟踪

    06-21 22:21:11.427: E/AndroidRuntime(14643): FATAL EXCEPTION: main
06-21 22:21:11.427: E/AndroidRuntime(14643): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.T4J_OAuth.activities/com.T4J_OAuth.activities.ExampleTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.T4J_OAuth.activities/com.T4J_OAuth.activities.feedbackActivity}: java.lang.NullPointerException
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.os.Looper.loop(Looper.java:150)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.main(ActivityThread.java:4293)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at java.lang.reflect.Method.invoke(Method.java:507)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at dalvik.system.NativeStart.main(Native Method)
06-21 22:21:11.427: E/AndroidRuntime(14643): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.T4J_OAuth.activities/com.T4J_OAuth.activities.feedbackActivity}: java.lang.NullPointerException
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1656)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.widget.TabHost.setCurrentTab(TabHost.java:326)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.widget.TabHost.addTab(TabHost.java:216)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at com.T4J_OAuth.activities.ExampleTabActivity.onCreate(ExampleTabActivity.java:354)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
06-21 22:21:11.427: E/AndroidRuntime(14643):    ... 11 more
06-21 22:21:11.427: E/AndroidRuntime(14643): Caused by: java.lang.NullPointerException
06-21 22:21:11.427: E/AndroidRuntime(14643):    at com.T4J_OAuth.activities.feedbackActivity.onCreate(feedbackActivity.java:85)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
06-21 22:21:11.427: E/AndroidRuntime(14643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
06-21 22:21:11.427: E/AndroidRuntime(14643):    ... 20 more

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:overScrollMode="always" 
        android:isScrollContainer="true" 
        android:scrollbarAlwaysDrawVerticalTrack="true" 
        android:scrollbarStyle="outsideInset" 
        android:scrollbars="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="5dp"
    android:text="Unit Name: Data Structurs"
    android:textSize="20dp" />



<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:text="Lecture Name: Queues"
    android:textSize="15dp" />


<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>    
</ScrollView>

提前致谢。

4

0 回答 0