【问题标题】:Attempt to invoke virtual method 'void android.widget.TextView.setText尝试调用虚拟方法 'void android.widget.TextView.setText
【发布时间】:2015-03-23 19:43:37
【问题描述】:

我正在片段中制作列表视图。以下是我的代码:

TitleQuote.java

package com.example.ishan.fragmentquote;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class QuoteList extends ListActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_quote_list, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    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_quote_list, container, false);
            Intent intent = getActivity().getIntent();
            String quote = intent.getStringExtra(TitleQuote.INDEX);
            if(quote != null) {
                TextView tv = (TextView) rootView.findViewById(R.id.quoteView);
                tv.setText("ishan");
            }
            return rootView;
        }
    }
}

fragment_title_quote.xml

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/titleList"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />  

fragment_quote_list.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.ishan.fragmentquote.QuoteList$PlaceholderFragment"
android:weightSum="1">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/quoteView"
    android:textSize="30sp" />
</LinearLayout>  

listview.xml

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView" />
</LinearLayout>

当我在 android studio 上构建项目时,一切正常,我看不到任何错误/警告。当我在模拟器/手机上运行它时,我得到了

很遗憾,FragmentQuote 已停止。

logcat 错误指定:

2119-2119/com.example.ishan.fragmentquote E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.example.ishan.fragmentquote,PID:2119 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

【问题讨论】:

    标签: android android-layout android-fragments android-listview android-arrayadapter


    【解决方案1】:
    tv.setText("ishan");
    

    这里你的 textview 参考 tv 变空了。

    【讨论】:

    • 你能解释一下为什么吗?
    • 很可能是因为以下行:tools:context="com.example.ishan.fragmentquote.QuoteList$PlaceholderFragment" in fragment_quote_list.xml ??
    • 但我认为该错误来自特定于 arraylist 构造函数的标题活动,并且 tv.setText 是另一个活动(报价视图)的一部分,该活动应该在我单击一个标题列表项后开始,但是应用程序没有启动,这意味着问题出在第一个活动中,因为报价活动尚未开始
    • @IshanKhare 所以您认为问题出在您尚未发布的部分代码中?!那你为什么不发布相关代码呢?也许也发布完整的堆栈跟踪。
    • @ci_第一个活动是titlequote.java文件
    【解决方案2】:

    可能有两个原因:

    1. QuoteListActivity 的 onCreate 方法在哪里?所以上下文为空
    2. 我没有看到你调用你的PlaceholderFragment? 请注意onCreateView 只会在你实例化PlaceholderFragment 时被调用

    如果你有所有这些东西,那么请分享完整的代码。

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多