尝试调用虚方法' void android.widget.TextView.setText

时间:2015-03-23 19:43:38

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

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

  

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上构建项目时,Everythin工作正常,我看到没有错误/警告。当我在模拟器/手机上运行时,我得到了

  

不幸的是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)'

2 个答案:

答案 0 :(得分:0)

tv.setText("ishan");

这里你的textview引用tv变为空。

答案 1 :(得分:0)

可能有两个可能的原因:

  1. 您的QuoteList活动的创建方法在哪里?所以上下文为空
  2. 我没有看到您拨打PlaceholderFragment?请注意,只有在您实例化onCreateView
  3. 时才会调用PlaceholderFragment

    如果您拥有所有这些内容,那么请分享完整的代码。