【问题标题】:Textview.settext() giving nullpointer exceptionTextview.settext() 给出空指针异常
【发布时间】:2014-10-24 12:02:17
【问题描述】:

这是调用 textview 并获取意图的活动

package com.example.smartbrowser;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.widget.TextView;

public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView vw =(TextView) findViewById(R.id.text);

Intent intent =getIntent();

String message = intent.getStringExtra(Browseractivity.Message);


vw.setText(message);


setContentView(R.layout.activity_main);
    }


}

还有activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >


 <TextView

  android:id="@+id/text"

  android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:layout_alignParentLeft="true"

  android:layout_alignParentTop="true"


  />


</RelativeLayout>

为什么 MainActivity.java 中出现空指针异常?

【问题讨论】:

  • 让你打印消息到 toast.maketext 你必须在字符串中传递空值首先在 Toast.MakeText(getApplicationContext(),"message"+message,Toast.length_short).show 中打印他的消息();它会帮助你做到这一点
  • 你传递的 String null 值在 String Message 中并不实际......所以你创造了这个问题......我想你发现他的错误......

标签: android eclipse nullpointerexception


【解决方案1】:

您必须在初始化任何视图之前放置setContentView(R.layout.activity_main);。在这里,在设置您的内容视图之前初始化您的TextView,这就是它返回null 的原因。

@Override 
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView vw =(TextView) findViewById(R.id.text);

    Intent intent =getIntent();

    String message = intent.getStringExtra(Browseractivity.Message);
    vw.setText(message);
}

【讨论】:

    【解决方案2】:

    您应该始终在这种情况下设置您的内容视图,然后在您的示例中将任何小部件作为 Textview 播放

    @Override
    
    protected void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    TextView vw =(TextView) findViewById(R.id.text);
    
    Intent intent =getIntent();
    
    String message = intent.getStringExtra(Browseractivity.Message);
    
    
    vw.setText(message);
    
    
    
        }
    
    
    }
    

    这应该可以解决问题:) 希望对你有帮助

    【讨论】:

      【解决方案3】:

      改成

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); // should be first
      TextView vw =(TextView) findViewById(R.id.text); // then initialize textview
      Intent intent =getIntent();
      String message = intent.getStringExtra(Browseractivity.Message);
      vw.setText(message);
      }
      

      你需要先给activity设置布局的内容,然后再初始化你的views。

      findViewById 在当前的膨胀布局中查找视图。你得到了NullPointerException,因为你在将布局设置为活动之前初始化了TextView

      【讨论】:

        【解决方案4】:

        我知道这是旧的,但我会回答它,因为此代码仍然适用。

        MainActivity.java

            public class MainActivity extends Activity {
        
        private TextView textView2; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main); // Change from activity_main > fragment_main
            textView2 = (TextView)findViewById(R.id.textview2);
            textView2.setOnClickListener(new View.OnClickListener() {
        
                @Override
                public void onClick(View textView2) {
                    // TODO Auto-generated method stub
        
                    if (textView2.isClickable() == true){
                        ((TextView) textView2).setText("Changed!!!!!!");                    
                    }
                }
            });
        
        
            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
            }
        }
        

        activity_main.xml

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        <!-- REMOVE android:id="@+id/container" -->
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.thinktankcoding.androiddevtest.MainActivity"
        tools:ignore="MergeRootFrame" />
        

        fragment_main.xml

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container" <!-- ADD THE LINE YOU REMOVED HERE AND REMOVE THESE COMMENTS -->
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.thinktankcoding.androiddevtest.MainActivity$PlaceholderFragment" >
        
        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <TextView
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:lines="2"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textview1"
            android:layout_below="@+id/textview1"
            android:clickable="true" />
        

        【讨论】:

        • 请避免只是粘贴代码,一点解释都不会有害。
        猜你喜欢
        • 1970-01-01
        • 2014-09-06
        • 2014-07-19
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多