1

对于我的活动,我有一个文本视图,我希望文本从 GridView 更改 onItemClick。对于我的生活,我无法弄清楚为什么当我单击 gridview 项目以更改 textview 的文本时它会不断创建 nullPointerException。“instructiontextview”是有问题的文本视图,我正在更改 onItemClick 底部的文本。

我设置了 contentView,所以它应该能够正确引用布局。我在 onCreate 中贴了它,所以应该没有引用问题。在这里真是不知所措。

活动.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jungle_activity);

final TextView instructiontextview = (TextView) findViewById(R.id.textview_jungle_instructions);

final GridView gridviewNeutrals = (GridView) findViewById(R.id.jungle_gridview_neutrals);

gridviewNeutrals.setAdapter(new JungleImageAdapterNeutral(
            getApplicationContext()));

gridviewNeutrals.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parentView, View imageview,
                int position, long id) {
            if (position == 0) {
                if (!JungleTimerService.baronJungleTimerStarted) {
                    Log.i(TAG, "baron jungle started");
                    JungleTimerService.baronJungleTimerStarted = true;
                    textviewArrayListIndex();
                    createBaronTimer();
                    mServer.baronJungleTimer();
                    TextView name = (TextView) imageview
                            .findViewById(R.id.jungle_grid_custom_text);
                    name.setTextSize(13);
                    FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
                            FrameLayout.LayoutParams.MATCH_PARENT,
                            FrameLayout.LayoutParams.MATCH_PARENT);
                    name.setLayoutParams(llp);
                    imageview
                            .setPadding(
                                    (int) getResources()
                                            .getDimensionPixelSize(
                                                    R.dimen.jungle_timer_button_pressed_dp),
                                    (int) getResources()
                                            .getDimensionPixelSize(
                                                    R.dimen.jungle_timer_button_pressed_dp),
                                    (int) getResources()
                                            .getDimensionPixelSize(
                                                    R.dimen.jungle_timer_button_pressed_dp),
                                    (int) getResources()
                                            .getDimensionPixelSize(
                                                    R.dimen.jungle_timer_button_pressed_dp));
                    instructiontextview.setText("worked");
                }
            }

日志猫

07-09 10:56:42.826: E/AndroidRuntime(7992): FATAL EXCEPTION: main
07-09 10:56:42.826: E/AndroidRuntime(7992): java.lang.NullPointerException
07-09 10:56:42.826: E/AndroidRuntime(7992):     at com.divinityworks.leaguecentral_leagueoflegends.JungleActivity$3.onItemClick(JungleActivity.java:164)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.widget.AbsListView.performItemClick(AbsListView.java:1268)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.widget.AbsListView$1.run(AbsListView.java:3950)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.os.Handler.handleCallback(Handler.java:615)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.os.Looper.loop(Looper.java:137)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at android.app.ActivityThread.main(ActivityThread.java:4950)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-09 10:56:42.826: E/AndroidRuntime(7992):     at dalvik.system.NativeStart.main(Native Method)

Jungle_activity.xml

 <ScrollView
    android:id="@+id/jungle_scrollview_timers"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10" >

    <FrameLayout
        android:id="@+id/framelayout_jungle_scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearlayout_jungle_inner_scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

        <TextView
            android:id="@+id/jungle_textview_instructions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:text="@string/textview_jungle_instructions"
            android:textSize="25sp" />
    </FrameLayout>
</ScrollView>

谢谢

4

1 回答 1

2

你有这个

final TextView instructiontextview =
(TextView) findViewById(R.id.textview_jungle_instructions);

但是你在 xml 中有这个

  <TextView
        android:id="@+id/jungle_textview_instructions" 

textview_jungle_instructions您的 xml 中没有带有 id 的 textview 。

由于 textview 未初始化,您将获得NullPointerException.

所以应该是

final TextView instructiontextview = 
(TextView) findViewById(R.id.jungle_textview_instructions);
于 2013-07-09T16:59:29.640 回答