片段中的Android按钮单击方法(崩溃)

Posted

技术标签:

【中文标题】片段中的Android按钮单击方法(崩溃)【英文标题】:Android Button Click Method In Fragment (Crash) 【发布时间】:2015-04-23 04:06:11 【问题描述】:

我的应用程序在启动时崩溃,崩溃的原因是以下代码中的按钮方法。我想知道我需要将方法放在哪里才能成功执行。

Fragment1.java

package ie.itsligo.medication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Fragment1 extends Fragment 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        // TODO Auto-generated method stub


        if (container == null)

            return null;
        

        Button btn = (Button) getView().findViewById(R.id.btnRespond);

         btn.setOnClickListener(new View.OnClickListener() 

                @Override
                public void onClick(View v) 
                    Toast.makeText(getActivity(), "You Are Now Active! Swipe Left For Patient Details & Medical Information", Toast.LENGTH_LONG).show();
                
         );

        return (LinearLayout)inflater.inflate(R.layout.fragment1_layout, container, false); 
    

fragment1_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="#D70B0D"
    android:orientation="vertical" >

     <TextView
        android:id="@+id/textView6"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="Emergency GPS Location"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_
        android:layout_
        android:layout_margin="5dp"/>

    <Button
        android:id="@+id/btnRespond"
        android:layout_
        android:layout_
        android:text="Respond To Call" 
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="@drawable/button_animation"
        android:padding="5dp"/>


</LinearLayout>

LogCat

02-21 13:43:36.224: D/AndroidRuntime(9141): Shutting down VM
02-21 13:43:36.224: W/dalvikvm(9141): threadid=1: thread exiting with uncaught exception (group=0x411072a0)
02-21 13:43:36.364: E/AndroidRuntime(9141): FATAL EXCEPTION: main
02-21 13:43:36.364: E/AndroidRuntime(9141): java.lang.NullPointerException
02-21 13:43:36.364: E/AndroidRuntime(9141):     at ie.itsligo.medication.Fragment1.onCreateView(Fragment1.java:25)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.View.measure(View.java:15288)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.View.measure(View.java:15288)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.View.measure(View.java:15288)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.View.measure(View.java:15288)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.View.measure(View.java:15288)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1974)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1217)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1390)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4481)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.Choreographer.doFrame(Choreographer.java:525)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.os.Handler.handleCallback(Handler.java:615)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.os.Looper.loop(Looper.java:137)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at android.app.ActivityThread.main(ActivityThread.java:4867)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
02-21 13:43:36.364: E/AndroidRuntime(9141):     at dalvik.system.NativeStart.main(Native Method)
02-21 13:43:36.424: D/dalvikvm(9141): GC_CONCURRENT freed 256K, 13% free 9479K/10823K, paused 15ms+4ms, total 62ms
02-21 13:43:48.536: I/Process(9141): Sending signal. PID: 9141 SIG: 9

【问题讨论】:

【参考方案1】:

问题是你在膨胀之前使用视图,所以你确实得到了 NullPointerException。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 

    View view = inflater.inflate(R.layout.fragment1_layout, container, false);
    Button btn = (Button) view.findViewById(R.id.btnRespond);

    btn.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View v) 
            Toast.makeText(getActivity(), "You Are Now Active! Swipe Left For Patient Details & Medical Information", Toast.LENGTH_LONG).show();
        
    );

    return view; 

【讨论】:

以上是关于片段中的Android按钮单击方法(崩溃)的主要内容,如果未能解决你的问题,请参考以下文章

如何从Android中的片段单击按钮打开片段

Android:单击片段中的按钮时如何通知活动? [复制]

Android 应用程序片段不断崩溃

从片段中的按钮单击打开导航抽屉(Kotlin)

如果在动画正在进行时单击,Android Fragment 事务动画会崩溃

安卓。通过从片段中的按钮调用片段中的方法来关闭片段?