【问题标题】:Null pointer exception with TextView on android with no apparent reasonandroid上TextView的空指针异常,没有明显的原因
【发布时间】:2013-02-19 03:24:11
【问题描述】:

我有一个非常烦人的错误,我不知道是什么原因造成的。它看到我在尝试从另一个具有 Intent 的活动启动活动时收到空指针异常。我尝试了我能想到的所有可能的解决方案,检查了数百次语法,尝试在新布局之前使用主布局设置内容视图..什么都没有! 我在这里需要一些紧急帮助。我在清单中声明了所有内容,以及布局 xml 文件。问题依赖于由主要活动调用的 ShowNoteActivity(来自 onItemClick() 方法):我从 logcat 得到 nullpointerexception 和一堆我不知道它们是什么的其他错误。

无论如何,这是相关代码,如果您需要更多,请告诉我。

public class MainActivity extends Activity implements OnClickListener ,OnItemClickListener{

    public final static String EXTRA_MESSAGE = "assaf.notepad.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseManager db = new DatabaseManager(this);
        String[]notes = db.listNotes();
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,notes);
        ListView lv = (ListView)findViewById(R.id.notes_list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        ImageButton addNoteBtn = (ImageButton)findViewById(R.id.add_note);
        addNoteBtn.setOnClickListener(this);

    }



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



    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, AddNoteActivity.class);
        startActivity(intent);
    }



    @Override
    public void onItemClick(AdapterView adapter, View view, int position, long id) {
        DatabaseManager db = new DatabaseManager(this);
        String[]notesContent = db.getNoteContent();
        Intent intent = new Intent(this,ShowNoteActivity.class);
        intent.putExtra(EXTRA_MESSAGE, notesContent[position]);
        startActivity(intent);



    }



}

这是有问题的活动!

public class ShowNoteActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = (TextView)findViewById(R.id.show_note);
        textView.setText(text);
        setContentView(R.layout.activity_show_note);
    }

}

这是日志猫

02-19 03:18:07.861: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-19 03:18:07.861: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to start activity ComponentInfo{assaf.notepad/assaf.notepad.ShowNoteActivity}: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 03:18:07.861: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
02-19 03:18:07.861: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 03:18:07.861: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 03:18:07.861: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
02-19 03:18:07.861: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
02-19 03:18:07.861: E/AndroidRuntime(829):  at assaf.notepad.ShowNoteActivity.onCreate(ShowNoteActivity.java:19)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.Activity.performCreate(Activity.java:5104)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 03:18:07.861: E/AndroidRuntime(829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 03:18:07.861: E/AndroidRuntime(829):  ... 11 more

相关的xml,“ShowNoteActivity”xml,带有问题的textView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/board"
        android:scaleType="fitXY"/>


    <Button
        android:id="@+id/edit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/edit_btn"
        android:textColor="@color/white"/>

    <Button
        android:id="@+id/delete_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/edit_btn"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:text="@string/delete_btn"
        android:textColor="@color/white"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/paper"
        android:layout_below="@id/edit_btn"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="30dp"/>

    <TextView
        android:id="@+id/show_note"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/edit_btn"
        android:layout_marginTop="110dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="45dp"
        android:layout_marginBottom="80dp"
        android:typeface="sans"
        android:textSize="10sp"/>




</RelativeLayou

t>

这不是数据库问题或类似问题,即使我尝试将“hello world”字符串传递给新活动,问题仍然存在。调用新活动时程序总是崩溃。

这是 xml 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="assaf.notepad"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="assaf.notepad.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="assaf.notepad.AddNoteActivity">

        </activity>
        <activity
            android:name="assaf.notepad.ShowNoteActivity">

        </activity>



    </application>

</manifest>

【问题讨论】:

  • "for no apparent reason..." -- 这是有原因的,相信我。继续查看空变量,看看您认为是在哪里创建它们的。
  • ShowNoteActivity 第19行是哪一行?
  • 你应该在拨打setContentView(R.layout.activity_show_note)之后拨打findViewById(R.id.show_note)
  • 没有帮助!现在试过了,仍然崩溃
  • 清理您的项目并再次运行它

标签: java android android-activity nullpointerexception


【解决方案1】:

问题是您在 ShowNoteActivity 中调用 setContentView 之前尝试访问 TextView。你的代码应该是:

setContentView(R.layout.activity_show_note);
TextView textView = (TextView)findViewById(R.id.show_note);
textView.setText(text);

【讨论】:

  • 我会尝试的,虽然我认为我之前尝试过但没有成功。但我会再试一次以确保
【解决方案2】:

试试

setContentView(R.layout.activity_show_note);    
Intent intent = getIntent();
String text = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = (TextView)findViewById(R.id.show_note);
if(textView!=null && text!=null)
{
    Log.d("Mytag","Everything is right");
    textView.setText(text);
}
else
{
if(textView==null)
    Log.d("Mytag","Textview is null");
else
    Log.d("Mytag","Text is null");
}

【讨论】:

  • 应用程序崩溃了吗?在第一个 if 条件下添加日志。
  • 是的,应用程序一直在我身上崩溃。它开始正常,但只有当我按下某个按钮(触发有问题的活动)时,程序才会崩溃
  • 现在我收到此错误 02-19 04:14:44.484: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{assaf.notepad/assaf.notepad. ShowNoteActivity}: android.view.InflateException: Binary XML file line #33: Error inflating class
  • @user2030118 你现在遇到了另一个问题。正如我一直说的,Update 使用新代码和 LogCat 来更新您的问题,以便 如果 这个问题得到解决,未来的读者不必搜索以找出所有问题。更不用说,您还让回答者蒙在鼓里。
  • 您如何评论除 ShowNoteActivity 类中的 setContentView 行之外的所有内容。如果它仍然崩溃,那么你的 xml 中有问题,因为上面的错误也说明了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 2016-07-03
相关资源
最近更新 更多