Java textView setText() NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24224285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
textView setText() NullPointerException
提问by David Hunsicker
I have an almost completely Vanilla App. All I'm trying to do is change the text of a textView, but it gives me a NullPointerException. I have no XML configuration, no added methods, nothing special. I checked my class, and it's pointing to the correct XML, and the XML does have a textView element in it with the correct id.
我有一个几乎完全原版的应用程序。我想要做的就是更改 textView 的文本,但它给了我一个 NullPointerException。我没有 XML 配置,没有添加方法,没有什么特别的。我检查了我的类,它指向正确的 XML,并且 XML 中确实有一个带有正确 ID 的 textView 元素。
I have also tried creating the textView instance outside of onCreate() on the class level, and the app just fails to start no matter what. I'm sure I'm overlooking simple, but I can't find the answer. Thanks for the help. EDIT: I realize that I need to move this code so that it accesses fragment resources, but when I do, it gives me an error:
我还尝试在类级别的 onCreate() 之外创建 textView 实例,但无论如何该应用程序都无法启动。我确定我忽略了简单,但我找不到答案。谢谢您的帮助。编辑:我意识到我需要移动此代码以便它访问片段资源,但是当我这样做时,它给了我一个错误:
Cannot make a static reference to the non-static method findViewById(int) from the type Activity
无法从类型 Activity 对非静态方法 findViewById(int) 进行静态引用
Where should I put my code if ALL of my views are in my fragment, and not in my Activity XML? or should I just have the activity load the fragment to begin with? package com.example.testapp;
如果我的所有视图都在我的片段中,而不是在我的活动 XML 中,我应该把代码放在哪里?或者我应该让活动加载片段开始?包 com.example.testapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Teststring");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).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 true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
ERROR LOG:
错误日志:
06-14 17:02:27.405: E/AndroidRuntime(1420): FATAL EXCEPTION: main
06-14 17:02:27.405: E/AndroidRuntime(1420): Process: com.example.testapp, PID: 1420
06-14 17:02:27.405: E/AndroidRuntime(1420): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: java.lang.NullPointerException
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread.access0(ActivityThread.java:135)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.os.Looper.loop(Looper.java:136)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-14 17:02:27.405: E/AndroidRuntime(1420): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 17:02:27.405: E/AndroidRuntime(1420): at java.lang.reflect.Method.invoke(Method.java:515)
06-14 17:02:27.405: E/AndroidRuntime(1420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-14 17:02:27.405: E/AndroidRuntime(1420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-14 17:02:27.405: E/AndroidRuntime(1420): at dalvik.system.NativeStart.main(Native Method)
06-14 17:02:27.405: E/AndroidRuntime(1420): Caused by: java.lang.NullPointerException
06-14 17:02:27.405: E/AndroidRuntime(1420): at com.example.testapp.MainActivity.onCreate(MainActivity.java:23)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.Activity.performCreate(Activity.java:5231)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-14 17:02:27.405: E/AndroidRuntime(1420): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-14 17:02:27.405: E/AndroidRuntime(1420): ... 11 more
XML fragment_main
XML fragment_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testapp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
采纳答案by Ryan
Can you post xml? It's likely that the id your java is assuming "R.id.textView1" is wrong. Maybe R.id.textview1?
可以发xml吗?您的 java 假设“R.id.textView1”的 id 可能是错误的。也许 R.id.textview1?
Well if your text view lives inside the fragment just do this:
好吧,如果您的文本视图位于片段中,请执行以下操作:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView tv = (TextView) rootView.findViewById(R.id.textView1);
tv.setText("Teststring");
return rootView;
}
回答by enissay
You are giving setContentView()
the wrong layout if your XML is actually declared as fragment_main
. That is why the controls are currently null
.
你给setContentView()
了错误的布局,如果你的XML作为实际声明fragment_main
。这就是为什么当前控件是null
.
// The layout file is not correct.
setContentView(R.layout.activity_second);
回答by rhoadster91
The TextView textView1 you are trying to refer is in the Fragment's inflated layout. The content view you have set in the Activity is activity_main.xml, which probably doesn't have the textView1 component.
您尝试引用的 TextView textView1 位于 Fragment 的膨胀布局中。您在 Activity 中设置的内容视图是 activity_main.xml,它可能没有 textView1 组件。
The solution is to override the Fragment's onViewCreated() method and set the text there
解决方案是覆盖 Fragment 的 onViewCreated() 方法并在那里设置文本
@Override
public void onViewCreated (View view, Bundle savedInstanceState)
{
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("My text");
/* Other code here */
}
Remember to call view.findViewById(R.id.textView1) and not just findViewById(R.id.textView1) because in the latter, you are trying to access the enclosing class non-static method in a static inner class. By calling view.findViewById(R.id.textView1) you are calling a method of a local variable in the static inner class.
请记住调用 view.findViewById(R.id.textView1) 而不仅仅是 findViewById(R.id.textView1) 因为在后者中,您试图访问静态内部类中的封闭类非静态方法。通过调用 view.findViewById(R.id.textView1) 您正在调用静态内部类中局部变量的方法。