【问题标题】:setText causes nullPointerException, very simple, but no idea (android)setText导致nullPointerException,很简单,但不知道(android)
【发布时间】:2014-04-24 18:54:43
【问题描述】:

我是 android 开发的新手,在这方面花了几个小时。 (日食+广告) 这里有一些额外的词,所以我可以发布这个。调用run_time.setText("t")时出现空指针异常;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_run);    
        TextView run_time = (TextView) findViewById(R.id.run_time);

        try {
            run_time.setText("t");
        }catch(Exception e) {
            Log.i("Log", e.getMessage()+"Error!"); // LogCat message
        }

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

    }

layout activity_start_run.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="horizontal" >

    <TextView 
    android:id="@+id/run_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ghjghjghjghjghjghj" />

</LinearLayout>

【问题讨论】:

  • 这个布局文件真的叫activity_start_run.xml吗?
  • 是的,文件名应该是正确的,很郁闷
  • 如果您 100% 肯定布局名称是这样的,请清理并重建以防 R 和二进制 XML 文件中的资源 ID 不同步。
  • 这个例外的解释是run_time实际上是在片段布局中而不是在活动布局中。
  • 它们在片段中,但我将它们复制到活动中,没有帮助。

标签: android eclipse nullpointerexception settext


【解决方案1】:

此空指针异常是因为您的资源未正确加载尝试清理项目并再次运行将解决。

R.id.run_time 这是从自动生成的文件 R.java 加载的。清理并重建项目将解决此问题。

MainActivity.java

package com.example.stacktest;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView run_time = (TextView) findViewById(R.id.run_time);
        try {
            run_time.setText("t");
        }catch(Exception e) {
            Log.i("Log", e.getMessage()+"Error!"); // LogCat message
        }
    }
}

和activity_main.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="horizontal" >

    <TextView
        android:id="@+id/run_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ghjghjghjghjghjghj" />

</LinearLayout>

【讨论】:

  • 您要发布多少份基本相同的答案?
  • 好的,我评论了这个: /** if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit() ; } */ 没有更多错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多