我正在创建我的第二个应用并遇到问题。 我的应用程序工作正常,无需输入按钮功能的代码。 但是当我这样做时,它崩溃了。该应用程序甚至无法打开。 发生了什么事?
我尝试重新排列,但仍然无法正常工作。
这些按钮用于组合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 timo24apps@gmail.com to request content
*/
private void sendRequestEmail(){
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:timo24apps@gmail.com"));
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 timo24apps@gmail.com to report problem
*/
private void sendEmail(){
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:timo24apps@gmail.com"));
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
答案 0 :(得分:1)
检查btn_clear
文件中是否存在activity_main.xml
个ID的按钮。如果是,请检查id声明是否如下所示:android:id="@+id/btn_clear"
*使用+
信号