[英]textView setText() NullPointerException
我有一個幾乎完全香草的應用程序。 我要做的就是更改textView的文本,但這給了我NullPointerException。 我沒有XML配置,沒有添加方法,沒有什么特別的。 我檢查了我的類,它指向正確的XML,並且XML中確實包含帶有正確ID的textView元素。
我也嘗試過在類級別的onCreate()外部創建textView實例,而該應用程序無論如何都無法啟動。 我確定我忽略了簡單的方法,但是找不到答案。 謝謝您的幫助。 編輯:我意識到我需要移動此代碼,以便它訪問片段資源,但是當我這樣做時,它給了我一個錯誤:
無法從活動類型靜態引用非靜態方法findViewById(int)
如果我所有的視圖都在我的片段中,而不是在我的Activity 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;
}
}
}
錯誤日志:
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.access$800(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片段_主要
<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>
您可以發布xml嗎? 您的Java所使用的ID可能假設“ R.id.textView1”是錯誤的。 也許是R.id.textview1?
好吧,如果您的文本視圖位於片段中,請執行以下操作:
@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;
}
如果您的XML實際上被聲明為fragment_main
則您給setContentView()
的布局錯誤。 這就是為什么控件當前為null
。
// The layout file is not correct.
setContentView(R.layout.activity_second);
您嘗試引用的TextView textView1位於Fragment的放大布局中。 您在“活動”中設置的內容視圖是activity_main.xml,它可能沒有textView1組件。
解決方案是重寫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 */
}
請記住要調用view.findViewById(R.id.textView1),而不僅僅是findViewById(R.id.textView1),因為在后者中,您試圖訪問靜態內部類中的封閉類非靜態方法。 通過調用view.findViewById(R.id.textView1),您可以在靜態內部類中調用局部變量的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.