按钮代码导致应用崩溃 - java

我正在创建第二个应用程序并且遇到问题。
我的应用程序可以正常运行,而无需输入代码以使按钮起作用。
但是当我这样做时,它崩溃了。该应用程序甚至无法打开。
到底是怎么回事?

我尝试重新排列,但仍然无法正常工作。
这些按钮用于组合EditText
它们在不同的应用程序上工作,但是当我尝试将其与此代码结合时会失败。

package com.glenn.howtowritefantasy.activities;
import com.glenn.howtowritefantasy.R;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements         
View.OnClickListener{
Button btn_Combine,btn_Clear;
EditText Tagline_p2,Start_writing_p1, 
Part_three1,Part_four1,Part_five1;
TextView tv_Result;


//THIS IS WHERE THE PROBLEM STARTS
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Tagline_p2= (EditText) findViewById(R.id.tagline_p2);
    Start_writing_p1= (EditText) findViewById(R.id.start_writing_p1);
    Part_three1= (EditText) findViewById(R.id.part_three1);
    Part_four1= (EditText) findViewById(R.id.part_four1);
    Part_five1= (EditText) findViewById(R.id.part_five1);
    tv_Result= (TextView) findViewById(R.id.tv_result);
    btn_Combine= (Button) findViewById(R.id.btn_combine);
    btn_Clear= (Button) findViewById(R.id.btn_clear);


    btn_Clear.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                btn_Clear.setBackgroundColor(Color.DKGRAY);
            } else if(event.getAction() == MotionEvent.ACTION_DOWN) {
                btn_Clear.setBackgroundColor(Color.BLUE);
            }
            return false;
        }

    });
    btn_Combine.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                btn_Combine.setBackgroundColor(Color.DKGRAY);
            } else if(event.getAction() == MotionEvent.ACTION_DOWN) {
                btn_Combine.setBackgroundColor(Color.BLUE);
            }
            return false;
        }

    });

//THIS IS WHERE THE PROBLEM ENDS

    // Check operating system version and set the layout file based on the outcome
    if(Build.VERSION.SDK_INT == 18 || Build.VERSION.SDK_INT == 19)
    {
        setContentView(R.layout.activity_main_no_material);
    }
    else
    {
        setContentView(R.layout.activity_main);
    }

    init();



}



@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.character_development:
            Intent intentCharDev = new Intent(this, CharacterDevActivity.class);
            startActivity(intentCharDev);
            break;
        case R.id.the_world:
            Intent intentWorld = new Intent(this, TheWorldActivity.class);
            startActivity(intentWorld);
            break;
        case R.id.the_magic:
            Intent intentMagic = new Intent(this, TheMagicActivity.class);
            startActivity(intentMagic);
            break;
        case R.id.creatures_races:
            Intent intentCreaturesRaces = new Intent(this, CreaturesRacesActivity.class);
            startActivity(intentCreaturesRaces);
            break;
        case R.id.story_development:
            Intent intentStoryDev = new Intent(this, StoryDevActivity.class);
            startActivity(intentStoryDev);
            break;
        case R.id.btn_combine:
            //Toast.makeText(MainActivity.this,"combine",Toast.LENGTH_SHORT).show();
            String first=Tagline_p2.getText().toString();
            String last=Start_writing_p1.getText().toString();
            String unew= Part_three1.getText().toString();
            String blast=Part_four1.getText().toString();
            String end= Part_five1.getText().toString();
            if (TextUtils.isEmpty(first)){
                Tagline_p2.setError("please enter your first name");
                return;
            }
            if (TextUtils.isEmpty(last)){
                Start_writing_p1.setError("please enter your last name");
                return;
            }
            if (TextUtils.isEmpty(unew)){
                Part_three1.setError("please enter your something");
                return;
            }
            if (TextUtils.isEmpty(unew)){
                Part_four1.setError("please enter your something");
                return;
            }
            if (TextUtils.isEmpty(unew)){
                Part_five1.setError("please enter your something");
                return;
            }
            String full=first+" "+last+" "+unew+" " +blast+" " +end;
            tv_Result.setText(""+full);

            break;
        case R.id.btn_clear:
            //Toast.makeText(MainActivity.this,"Clear",Toast.LENGTH_SHORT).show();
            Tagline_p2.setText("");
            Start_writing_p1.setText("");
            Part_three1.setText("");
            Part_four1.setText("");
            Part_five1.setText("");
            tv_Result.setText("");
            break;


    }

}


private void init(){
    this.setSupportActionBar((Toolbar) findViewById(R.id.tb_menu));
    this.getSupportActionBar().setTitle("Write Fantasy");

    findViewById(R.id.character_development).setOnClickListener(this);
    findViewById(R.id.the_world).setOnClickListener(this);
    findViewById(R.id.the_magic).setOnClickListener(this);
    findViewById(R.id.creatures_races).setOnClickListener(this);
    findViewById(R.id.story_development).setOnClickListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

/**
 * Handles events from the options menu
 */
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.item_1:
            sendRequestEmail();
            return true;
        case R.id.item_2:
            sendEmail();
            return true;
        case R.id.item_3:
            Intent intent = new Intent(this, AboutActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

/**
 * Sends email to [email protected] to request content
 */
private void sendRequestEmail(){
    Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:[email protected]"));
    i.putExtra(Intent.EXTRA_SUBJECT, "How to Write Fantasy: Request Content");
    i.putExtra(Intent.EXTRA_TEXT   , "Please describe what content you would like to see added or feel free to give me any suggestions for improving the app! I'm also available to read a chapter or two from your story and give some feedback!" + System.getProperty("line.separator") + System.getProperty("line.separator") + "**********************" + System.getProperty("line.separator") + System.getProperty("line.separator"));
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
}

/**
 * Sends email to [email protected] to report problem
 */
private void sendEmail(){
    Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:[email protected]"));
    i.putExtra(Intent.EXTRA_SUBJECT, "How to Write Fantasy: Report Problem");
    i.putExtra(Intent.EXTRA_TEXT   , "Please describe what problem you encountered or feel free to give me any suggestions for improving the app!" + System.getProperty("line.separator") + System.getProperty("line.separator") + "**********************" + System.getProperty("line.separator") + System.getProperty("line.separator"));
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
  }
}

这是logcat错误:

Process: com.glenn.howtowritefantasy, PID: 5705
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.glenn.howtowritefantasy/com.glenn.howtowritefantasy.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference

参考方案

检查btn_clear文件中是否存在具有activity_main.xml id的按钮。如果是这样,请检查id声明是否像这样:android:id="@+id/btn_clear" *使用+信号

Android Firebase:将数据保存到数据库 - java

我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

如何在Wiremock中为JUNIT匹配精确的json - java

我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares�…

AppCompat不支持当前主题 - java

我的应用在Android N上运行正常,但在Android M上的setContentView(R.layout.activity_main)崩溃: Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { w…