对于我的Activity,我有一个textview,我希望文本从GridView更改onItemClick。对于我的生活,当我单击gridview项以更改textview的文本时,我无法弄清楚为什么它一直在创建nullPointerException。 “instrucitontextview”是有问题的文本视图,我正在更改onItemClick底部的文本。
我有contentView设置,因此它应该能够正确引用布局。我在onCreate中进行了decalred,所以应该没有引用问题。真的在这里不知所措。
Activity.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");
}
}
logcat的
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>
由于
答案 0 :(得分:2)
你有这个
final TextView instructiontextview =
(TextView) findViewById(R.id.textview_jungle_instructions);
但你有这个在xml
<TextView
android:id="@+id/jungle_textview_instructions"
您的xml中没有标识为textview_jungle_instructions
的文本视图。
由于未初始化textview,您将获得NullPointerException
。
所以它应该是
final TextView instructiontextview =
(TextView) findViewById(R.id.jungle_textview_instructions);