2014-04-03 51 views
0

我是Android新手,请原谅我的无知。我试图搜索论坛,但一直没有找到任何可以使之发挥作用的东西。我试图使用.getText()。toString()(见下文),但得到一个java.lang.NullPointerException错误。我改变了我的setContentView,以便它可以查看fragment_main.xml,但仍然为我的不同组件返回null。这个目标非常简单,只是为了改变TextView上的文本,因为我刚刚开始。任何帮助,将不胜感激。尝试getText时出现空指针错误

package com.example.app; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.os.Build; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_main); 

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

     Button changeTxt = (Button) findViewById(R.id.btnChangeText); 
     final TextView textView = (TextView) findViewById(R.id.txtMessage); 

     changeTxt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String text = textView.getText().toString(); 

       if (text.contains("World")) { 
        textView.setText("Hello Android!"); 
       } 
       else { 
        textView.setText(("Hello World!")); 
       } 
      } 
     }); 
    } 


    public void onStart(){ 
     super.onStart(); 
    } 

    public void onResume(){ 
     super.onResume(); 
    } 

    public void onPause(){ 
     super.onPause(); 
    } 

    public void onStop(){ 
     super.onStop(); 
    } 

    public void onDestroy(){ 
     super.onDestroy(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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(); 
     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_main, container, false); 
      return rootView; 
     } 
    } 
} 

这里是XML:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/txtMessage" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Change Text" 
     android:id="@+id/btnChangeText" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

Fllo,更新的代码:

XML fragment_main.xml

​​

主要活动

package com.example.app; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.os.Build; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_main); 

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


    } 


    public void onStart(){ 
     super.onStart(); 
    } 

    public void onResume(){ 
     super.onResume(); 
    } 

    public void onPause(){ 
     super.onPause(); 
    } 

    public void onStop(){ 
     super.onStop(); 
    } 

    public void onDestroy(){ 
     super.onDestroy(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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(); 
     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_main, container, false); 

      Button changeTxt = (Button) findViewById(R.id.btnChangeText); 
      final TextView textView = (TextView) findViewById(R.id.txtMessage); 

      changeTxt.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) 
       { 
        String text = textView.getText().toString(); 

        if (text.contains("World")) 
        { 
         textView.setText("Hello Android!"); 
        } 
        else 
        { 
         textView.setText(("Hello World!")); 
        } 
       } 
      }); 

      return rootView; 
     } 
    } 

} 
+0

摆脱如果您的活动中的一部分因为我没有在xml中看到一个视图组 – Raghunandan

+1

布局文件的名称是什么? (你可能在错误的布局中使用了findViewById) – DigCamara

+0

你也膨胀了你设置为在fragment中活动的相同的'fragment_main'布局。发布堆栈跟踪。该活动可能属于'activity_main.xml' – Raghunandan

回答

2

NullPointerException异常发生的原因,似乎你有你的Activity和你Fragment相同的布局:

// Activity 
setContentView(R.layout.fragment_main); 

// Fragment 
View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
return rootView; 

你需要检查你的意见(TextView的&按钮)是在正确的布局,而不是进入aonther(我猜activity_main)。

两种解决方案:

  • 复制/粘贴你的观点里fragment_mainactivity_main和改变setContentView(R.layout.activity_main)

  • 或者让你的观点里fragment_main和使用下面的代码,以找到您Fragment里面你的看法,具体如下:

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
        View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        Button changeTxt = (Button) rootView.findViewById(R.id.btnChangeText); 
        final TextView textView = (TextView) rootView.findViewById(R.id.txtMessage); 
    
        changeTxt.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          String text = textView.getText().toString(); 
    
          if (text.contains("World")) { 
           textView.setText("Hello Android!"); 
          } 
          else { 
           textView.setText(("Hello World!")); 
          } 
         } 
        }); 
        return rootView; 
    } 
    

希望这有助于。


UPDATE:

1。在fragment_main内删除您的FrameLayout。创建一个名为activity_main新的布局,并添加的FrameLayout进去(用ID)为:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/container" /> 

2。设置此布局onCreate方法到您的活动像:

// onCreate method Activity 
setContentView(R.layout.activity_main); 

3。然后,删除static片段声明,这应该是:

public class PlaceholderFragment ... { } 

4。最后,当你试图找到里面onCreateView方法ID的元素,你必须始终使用膨胀的观点为:

rootView.findViewById(R.id.some_id_example); 

瞧!这应该现在工作。

+0

我尝试将代码从onCreate粘贴到onCreateView,但它在findViewByID上抛出一个错误。 “非静态方法不能从静态上下文中引用” – jshield

+0

@ user1019838你应该更新你的问题并发布你的新代码。我会帮你的,我认为这不是很重要的调试;) - 也许你忘了添加充气视图到你的findViewById方法。看到我的回答,你应该做'rootView.findViewById(...)' – Fllo

+0

我不需要移除'PlaceholderFragment'的静态声明,它仍然有效,谢谢!但是,是否有更新日志可以让我们更多地了解这些变化?许多较旧的教程不包括片段,而ADT似乎生成这些_different_项目,使学习变得困难...... –