gpt4 book ai didi

android - onCreateView后TextView为null : nullpointerexception

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:32 24 4
gpt4 key购买 nike

我已经尝试了很多提交给堆栈溢出的解决方案,但到目前为止没有一个对我有帮助。当在另一个调用父 Activity 的 fragment 中触发按钮监听器时,就会出现问题。父 Activity 调用此 fragment updateValue() 方法(见下文)。在我的 fragment 的 onCreateView() 中,我有以下代码:

Result.class 扩展 Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.result, container, false);
resultTV = (TextView)view.findViewById(R.id.resultTV);
resultTV.setText(resultTV.getText().toString() + "test!");
return view;
}

这非常有效。但是,当我从父 Activity 调用 fragment 中的方法时,textview 引用为空(空指针异常)。异常发生在调用 resultTV.setText() 的地方。

public void updateValue(View v) {
String text = ((Button)v).getText().toString();

if(text.toLowerCase() == "c") resultTV.setText(DEFAULT_RESULT_TEXT);
else resultTV.setText(resultTV.getText().toString() + text);
}

异常

10-09 16:32:59.091: E/AndroidRuntime(2324): FATAL EXCEPTION: main
10-09 16:32:59.091: E/AndroidRuntime(2324): java.lang.NullPointerException
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Result.updateValue(Result.java:53)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Main.onBasicButtonClicked(Main.java:65)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.test.Basic$7.onClick(Basic.java:120)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.view.View.performClick(View.java:4240)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.view.View$PerformClick.run(View.java:17721)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Handler.handleCallback(Handler.java:730)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.os.Looper.loop(Looper.java:137)
10-09 16:32:59.091: E/AndroidRuntime(2324): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-09 16:32:59.091: E/AndroidRuntime(2324): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:32:59.091: E/AndroidRuntime(2324): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-09 16:32:59.091: E/AndroidRuntime(2324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 16:32:59.091: E/AndroidRuntime(2324): at dalvik.system.NativeStart.main(Native Method)

编辑:

Main 有几个 fragment :basicscientific fragment 只是按钮。 matrix fragment 尚未使用,因此我们可以忽略它。 result fragment 是找到相关 TextView 的地方。

Main.class(主要 Activity 类)

public class Main extends Activity implements 
OnBasicButtonClickedListener,
OnScientificButtonClickedListener,
OnMatrixButtonClickedListener {

private FragmentManager fm = getFragmentManager();
private FragmentTransaction ft = fm.beginTransaction();
private Basic basic = new Basic();
private Scientific scientific = new Scientific();
private Result result = new Result();
private MatrixFragment matrixFrag = new MatrixFragment();
private static final String LOG = "Main.class";

/* this list view will hold all submitted formulas */
private ListView formulaLV;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if(findViewById(R.id.result) == null)
ft.add(R.id.result, result);

if(findViewById(R.id.basic) == null)
ft.add(R.id.basic, basic);

if(findViewById(R.id.scientific) == null)
ft.add(R.id.scientific, scientific);

if(findViewById(R.id.matrix) == null)
ft.add(R.id.matrix, matrixFrag);
ft.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return false;
}

/*
* This method acts as a listener for fragment to fragment communication
* from Basic fragment.
*/
public void onBasicButtonClicked(View v) {
Log.i(LOG, ((Button)v).getText() + " button pressed!");
result.updateValue(v);
}

/*
* This method acts as a listener for fragment to fragment communication
* from Scientific fragment.
*/
public void onScientificButtonClicked(View v) {
Log.i(LOG, ((Button)v).getText() + " button pressed!");
result.updateValue(v);
}

@Override
public void matrixButtonClicked(View view) {
// matrix button clicked

}

最佳答案

空指针可能是因为 resultTV 没有显示给 updateValue() 方法。您如何在 fragment 类中声明 resultTV?如果您要实例化该类的多个实例并且希望它随着时间的推移保持不变,您可能想尝试将其声明为静态

你也可以试试

TextView resultTV = (TextView)view.findViewById(R.id.resultTV);

updateValue 方法中

关于android - onCreateView后TextView为null : nullpointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19282719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com