2016-02-22 100 views
-1

我的应用程序出现问题,但是当我点击“选项”按钮时,它会停止工作并退出应用程序。当我点击某个按钮时,Android App停止工作

有人可以帮助我吗?

第一代码是从options.java类和第二代码是从mainactivity

包com.example.sportsquizzone;

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

/** 
* Created by Usman on 22/02/2016. 
*/ 
public class options extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options); 
} 
public void onButtonClick(View view) { 
    Intent b = new Intent(this, MainActivity.class); 
    b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(b); 

} 

public void btnAbout(View view) { 
    AlertDialog.Builder About = new AlertDialog.Builder(this); 
    About.setMessage("* Developed by Usman Saddique \n* Developed on  \n* This game runs on Android version 4.2 and above") 
      .setPositiveButton("Close", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }) 
      .setTitle("About") 
      .setIcon(R.drawable.question_mark) 
      .create(); 
    About.show(); 
} 
} 

package com.example.sportsquizzone;

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 


} 

@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; 
} 

public void onButtonClick(View v) 
{ 
    if(v.getId() == R.id.btnPlay) 
    { 
     Intent i = new Intent(MainActivity.this, level.class); 
     startActivity(i); 
    } 

    if(v.getId() == R.id.btnOptions) 
    { 
     Intent i = new Intent(MainActivity.this, option.class); 
     startActivity(i); 
    } 

} 

@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); 
} 

public void Instructions(View view) { 
final AlertDialog.Builder Instructions = new AlertDialog.Builder(this); 
Instructions.setMessage("The SQZ is designed to test your knowledge across variety of sports, to play: \n1. Click Play \n2. Select Dificulty \n3. when quiz begins, four answers will be provided select the correct one.  ") 
    .setPositiveButton("Got It", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }) 
      .setTitle("Instructions") 
      .setIcon(R.drawable.question_mark) 
      .create(); 
    Instructions.show(); 

} 

public void btnQuit(View view) { 

    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.warning) 
    .setTitle("Closing Application") 
    .setMessage("Are you sure you want to Quit?") 
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     finish(); 
    } 

}) 
.setNegativeButton("No", null) 
.show(); 
} 
} 
+1

在寻求崩溃帮助时,从logcat发布完整堆栈跟踪。 –

+0

你有没有在manifest上声明你的选项类? – statosdotcom

+0

谢谢你“statosdotcom”我完全忘了将它添加到清单,完成后它正在工作。谢谢大家的帮助 – Ozzy

回答

0

我看不到你的logcat错误,但我认为你已经分别创建了“option”类和它的布局。因此,如果这是您所做的事情,可能您已经忘记声明清单上的活动: 将其包含在您的清单中: