2017-12-19 13 views
-1

私は2番目のアプリケーションを作成していますが、問題があります。 ボタンを機能させるためのコードを入力しなくても、アプリは正常に動作します。 しかし私がするとき、それはクラッシュします。アプリは開かない。 何が起こっていますか?ボタンコードがクラッシュするアプリケーション

私は再配置を試みましたが、それはまだ動作しません。 ボタンを組み合わせて使用​​しています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 
+1

あなたの 'logcat'またはエラーを貼り付けてください。 – meditat

+0

見つけよう。私は上に貼り付けます。 1つの瞬間をしてください –

+0

大丈夫私はそれを貼り付け –

答えて

1

チェックbtn_clear idのボタンがあなたのactivity_main.xmlファイルに存在する場合。もしそうなら、IDの宣言が次のようになっているか確認してください。android:id="@+id/btn_clear" * +シグナルの場合

+0

良いヒント。私がチェックします。その後、あなたに戻ってください。 –

+0

okはクラッシュを修正しました。私の編集テキストは合成されていませんが、最初に私は自分でそれを行うことができますか?本当にありがとう!! –

+0

ニース!あなたの研究に幸運! – frauderics

関連する問題