单击按钮后应用程序崩溃

时间:2016-06-23 10:16:26

标签: java android

我尝试创建一个简单的应用程序" MemeCrater"这是我从youtube教程中学到的。但每当我按下“创建”按钮时。应用程序崩溃。

这是我得到的错误。 的错误

Process: com.example.iemshekhar.memegenerator, PID: 13118
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.iemshekhar.memegenerator.TopSectionFragment$TopSectionListener.createMeme(java.lang.String, java.lang.String)' on a null object reference
        at com.example.iemshekhar.memegenerator.TopSectionFragment.buttonClicked(TopSectionFragment.java:57)
        at com.example.iemshekhar.memegenerator.TopSectionFragment$1.onClick(TopSectionFragment.java:49)
        at android.view.View.performClick(View.java:5697)
        at android.widget.TextView.performClick(TextView.java:10815)
        at android.view.View$PerformClick.run(View.java:22526)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7229)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我的主要活动java文件:

MainActivity.java

package com.example.iemshekhar.memegenerator;

import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements TopSectionFragment.TopSectionListener {

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

    @Override
    public void createMeme(String top, String bottom) {
        BottomPictureFragment bottmfragment=(BottomPictureFragment)getFragmentManager().findFragmentById(R.id.fragment2);
        bottmfragment.setMemeText(top,bottom);
    }

    @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_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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的顶级Fragement java文件:

TopSectionFragment.java

package com.example.iemshekhar.memegenerator;


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TopSectionFragment extends Fragment {
    private static EditText top, bottom;


    TopSectionListener activitycommander;
    public interface TopSectionListener{
        public void createMeme(String top,String bottom);
    }


    public void onAtttach(Context context)
    {
        try{
            activitycommander=(TopSectionListener)context;
        }
        catch(ClassCastException e){
            throw new ClassCastException(e.getMessage());
        }



    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_section_fragment, container, false);

        top = (EditText)view.findViewById(R.id.toptextinput);
    bottom = (EditText) view.findViewById(R.id.bottomtextinput);

       final Button create = (Button) view.findViewById(R.id.createbutton);
        create.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                       buttonClicked(v);
                    }
                }
        );
        return view;
    }

    public void buttonClicked(View v) {
    activitycommander.createMeme(top.getText().toString(),bottom.getText().toString());
    }
}

这个文件是我的底部片段java文件:

BottomSectionFragment.java

package com.example.iemshekhar.memegenerator;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by IEmShekhar on 6/18/2016.
 */
public class BottomPictureFragment extends Fragment {

    public static TextView toptext,bottomtext;

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


        View view = inflater.inflate(R.layout.bottom_picture_fragment, container, false);

        toptext=(TextView)view.findViewById(R.id.textView1);
        bottomtext=(TextView)view.findViewById(R.id.textView2);

        return view;
    }

    public void setMemeText(String top,String bottom){
        toptext.setText(top);
        bottomtext.setText(bottom);
    }
}

这些是我的XML文件:

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="#006669"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="290dp"
        android:name="com.example.iemshekhar.memegenerator.BottomPictureFragment"
        android:id="@+id/fragment2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/bottom_picture_fragment" />

    <fragment
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:name="com.example.iemshekhar.memegenerator.TopSectionFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/top_section_fragment"
        android:layout_above="@+id/fragment2" />

</RelativeLayout>

Top_section_Fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" android:background="#999999">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toptextinput"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:width="300dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bottomtextinput"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/toptextinput"
        android:width="300dp"
        android:layout_marginTop="30dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:layout_centerHorizontal="true"
        android:text="@string/Create_text"
        android:background="#006666"
        android:textStyle="bold"
        android:id="@+id/createbutton"/>

</RelativeLayout>

Bottom_picture_Fragement.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/afaque">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/toptext"
        android:textStyle="bold"
        android:textSize="25sp"
        android:background="#ffff"
        android:width="400dp"
        android:textAlignment="center"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/bottomtext"
        android:textStyle="bold"
        android:textSize="25sp"
        android:background="#ffff"
        android:width="400dp"
        android:textAlignment="center"
        android:id="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

猜测您是否尝试从editText设置文字,您应该从TopSectionFragment

中的editText访问该文字

替换

//Initialize
`private static TextView top, bottom;`

//onCreateView() Method
top = (TextView) view.findViewById(R.id.textView1);
bottom = (TextView) view.findViewById(R.id.textView2);

用这个

//Initialize
`private static EditText top, bottom;`

//onCreateView Method

//onCreateView() Method
top = (EditText) view.findViewById(R.id.toptextinput);
bottom = (EditText) view.findViewById(R.id.bottomtextinput);
TopSectionFragment 中的

。希望这会有所帮助:)