通过更改textview文本的NullPointerException

时间:2014-06-13 09:45:43

标签: android nullpointerexception textview settext

我正在尝试更改Textview的文本,但是当尝试使用findViewById获取Textview时,我得到Nullpointerexception。我用调试器检查了它,导致Nullpointerexception的行是:TextView txt = (TextView) findViewById(R.id.txt);

看起来我无法使用findViewById方法获取任何东西,因为它们总是返回null值。在每个表格上。

也许有一个我错了的设置,如果有人可以告诉我吗?

我的xml:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.firstapp.ZoekVelg$PlaceholderFragment" 
    android:id="@+id/ZoekVelgLL">

    <Button
        android:id="@+id/button_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="BekijkDetailPage"
        android:text="Bekijk details" />

    <TextView
        android:id="@+id/txt"
        android:text="gewoon tekst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

我的代码:

public class ZoekVelg extends ActionBarActivity {

    String merk, model, uitvoering;

    TextView textview;

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

        Intent intent = getIntent();
        merk = intent.getStringExtra("merk");
        model = intent.getStringExtra("model");
        uitvoering = intent.getStringExtra("uitvoering");

        TextView txt = (TextView) findViewById(R.id.txt); //<-- line that returns null
        txt.setText("even updaten");


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

我的LogCat:

06-13 11:13:10.136: W/dalvikvm(32370): threadid=1: thread exiting with uncaught exception (group=0x416a1e18)
06-13 11:13:10.146: E/AndroidRuntime(32370): FATAL EXCEPTION: main
06-13 11:13:10.146: E/AndroidRuntime(32370): Process: com.example.firstapp, PID: 32370
06-13 11:13:10.146: E/AndroidRuntime(32370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstapp/com.example.firstapp.ZoekVelg}: java.lang.NullPointerException
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.os.Looper.loop(Looper.java:157)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread.main(ActivityThread.java:5872)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at java.lang.reflect.Method.invoke(Method.java:515)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at dalvik.system.NativeStart.main(Native Method)
06-13 11:13:10.146: E/AndroidRuntime(32370): Caused by: java.lang.NullPointerException
06-13 11:13:10.146: E/AndroidRuntime(32370):    at com.example.firstapp.ZoekVelg.onCreate(ZoekVelg.java:33)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.Activity.performCreate(Activity.java:5312)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
06-13 11:13:10.146: E/AndroidRuntime(32370):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
06-13 11:13:10.146: E/AndroidRuntime(32370):    ... 11 more

0 个答案:

没有答案