主活动中onCreate()方法中的null对象引用错误

时间:2016-07-01 06:41:08

标签: java android-fragments

在处理片段时,我收到了此错误。我已经环顾了很多,这里,各种教程和所有,但我似乎无法分辨,我出错了...我知道这个问题已被反复询问所有,但帮助我!

提前致谢! PS:之前没有发过问题,所以,也提前抱歉!

  1. 主要活动

     public class MainActivity extends FragmentActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if(savedInstanceState==null)
        {
            getSupportFragmentManager().beginTransaction().add(R.id.fragment,new MainActivityFragment()).commit();
        }
    
    }
    
  2. 片段活动

       public class MainActivityFragment extends Fragment {
    
           CalendarView cv;
           Context context;
    
           public MainActivityFragment() {
           }
    
           @Override
           public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               //getActivity().setContentView(R.layout.fragment_main);
           }
    
           @Override
           public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
               View view = inflater.inflate(R.layout.fragment_main, container, false);
    
    
    
           EditText editText = (EditText) view.findViewById(R.id.editText);
           editText.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                getCalendarView();
               }
           });
    
           return view;
       }
    
  3. MainActivity布局

    <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: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.ipt02.spotchecktrial1.MainActivity"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Is this coming?"/>
    
    
    
     <fragment
        android:id="@+id/fragment"
        class="com.example.ipt02.spotchecktrial1.MainActivityFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    </RelativeLayout>
    
  4. 最后是logcat:

  5. java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.ipt02.spotchecktrial1/com.example.ipt02.spotchecktrial1.MainActivity}:
    java.lang.NullPointerException: Attempt to invoke interface method
    'java.lang.String java.lang.CharSequence.toString()' on a null object
    reference at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
    at android.app.ActivityThread.access$800(ActivityThread.java:151) at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5257)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
             Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
    at android.content.res.TypedArray.getString(TypedArray.java:176)
                    at android.view.View.<init>(View.java:3992)
                    at android.view.ViewGroup.<init>(ViewGroup.java:497)
                    at android.view.ViewGroup.<init>(ViewGroup.java:493)
                    at android.view.ViewGroup.<init>(ViewGroup.java:489)
                    at android.view.ViewGroup.<init>(ViewGroup.java:485)
                    at android.widget.FrameLayout.<init>(FrameLayout.java:103)
                    at com.android.internal.policy.impl.PhoneWindow$DecorView.<init>(PhoneWindow.java:2226)
     at com.android.internal.policy.impl.PhoneWindow.generateDecor(PhoneWindow.java:3316)
    at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3669)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:368)
    at android.app.Activity.setContentView(Activity.java:2145) 
    at com.example.ipt02.spotchecktrial1.MainActivity.onCreate(MainActivity.java:22)
                    at android.app.Activity.performCreate(Activity.java:5990)
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5257)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at java.lang.reflect.Method.invoke(Method.java:372)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    

    编辑 - 添加了Strings.XML文件

     <string name="hello_blank_fragment">Hello blank fragment</string>
    <string-array name="location">
        <item>Amsterdam</item>
        <item>Dubai</item>
        <item>Great Coral Reef</item>
    </string-array>
    
    <string-array name="shift">
        <item>Shift - A</item>
        <item>Shift - B</item>
    </string-array>
    
    <string-array name="disciples">
        <item>Sammy</item>
        <item>John</item>
        <item>Arry</item>
        <item>Voldy</item>
    </string-array>
    <string name="title_activity_main">MainActivity</string>
    

1 个答案:

答案 0 :(得分:0)

MainActivity使用这种方式

Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fm = getSupportFragmentManager();
    fragment = fm.findFragmentByTag("myFragmentTag");
    if (fragment == null) {
        FragmentTransaction ft = fm.beginTransaction();
        fragment =new MyFragment();
        ft.add(android.R.id.content,fragment,"myFragmentTag");
        ft.commit();
    }

}