[英]Access TextView in Layout from Fragment
诚然,我对 Android 开发和 Java 总体上还是很陌生,在我看到很多其他人也在搜索的东西上遇到了一些问题,但其他解决方案似乎并不适合我。 我在另一个文件中使用了这个确切的代码,将其复制到另一个程序(从 NavigationDrawer 切换到 NavigationView),现在 Fragment 在尝试访问 TextView 时给了我一个 NullPointerException。
据我所知,在尝试调用findViewById
之前,我已经定义了视图。 我绝对确定问题出在方法show_life1_total()
TextView life1_total = (TextView) view.findViewById(R.id.life1_total);
但我无法弄清楚该行有什么问题,因为它在不同的文件中完美运行。 非常感激任何的帮助。
或者,如果我应该以其他方式处理片段中的按钮和视图,请赐教。 它们在活动中真的很容易,但是 NavigationView 迫使我使用片段,并且为我打算在片段中使用的每个按钮设置onClickListeners
看起来这将是大量不必要的工作。 也许我错过了一些明显的东西。
这是我的片段
package com.android4dev.navigationview;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Admin on 04-06-2015.
*/
public class GameFragment extends Fragment implements View.OnClickListener{
public static Integer nlife1Total=20;
public static Integer nlife2Total=20;
View view;
Button life1_minus;
Button life1_plus;
Button life2_minus;
Button life2_plus;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_life_counter,container,false);
life1_minus = (Button) view.findViewById(R.id.life1_minus);
life1_minus.setOnClickListener(this);
life1_plus = (Button) view.findViewById(R.id.life1_plus);
life1_plus.setOnClickListener(this);
life2_minus = (Button) view.findViewById(R.id.life2_minus);
life2_minus.setOnClickListener(this);
life2_plus = (Button) view.findViewById(R.id.life2_plus);
life2_plus.setOnClickListener(this);
show_life1_total();
show_life2_total();
return view;
}
public void startNewGame()
{
nlife1Total=20;
nlife2Total=20;
//show_life1_total();
//show_life2_total();
}
public void show_life1_total() {
TextView life1_total = (TextView) view.findViewById(R.id.life1_total);
String display_life1_total = String.valueOf(nlife1Total);
life1_total.setText(display_life1_total);
}
public void show_life2_total() {
TextView life2_total = (TextView) view.findViewById(R.id.life2_total);
String display_life2_total = String.valueOf(nlife2Total);
life2_total.setText(display_life2_total);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.life1_minus:
nlife1Total--;
show_life1_total();
break;
case R.id.life1_plus:
nlife1Total++;
show_life1_total();
break;
case R.id.life2_minus:
nlife2Total--;
show_life2_total();
break;
case R.id.life2_plus:
nlife2Total++;
show_life2_total();
break;
}
}
}
来自 xml 文件的相关视图(这是在 fragment_life_counter.xml 中)
<TextView
android:id="@+id/life1_total"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="140sp"
android:text="@string/life1_total"
android:fontFamily="sans-serif-thin"
android:textColor="@color/life_color"
android:background="@drawable/backdrop_ubr_grixis" />
随着logcat
08-27 22:17:46.103 18943-18943/com.android4dev.navigationview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android4dev.navigationview, PID: 18943
java.lang.NullPointerException
at com.android4dev.navigationview.GameFragment.show_life1_total(GameFragment.java:69)
at com.android4dev.navigationview.GameFragment.onCreateView(GameFragment.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
您已经在方法中声明了一个视图,它将充当本地变量,它将被销毁。 让它成为一个全局的或者你已经声明了一个全局的 View 变量来初始化它。
按照这个——
代替 -
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_life_counter,container,false);
删除视图,因为您已经在类的顶部声明了它。
弄成这个样子——
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_life_counter,container,false);
请记住,您正在尝试访问未初始化的 show_life1_total() 方法中的视图对象。
我希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.