新警报对话框未显示安卓工作室



07-11 20:44:43.348 12614-12614/com.karanvir.search E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.karanvir.search, PID: 12614
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
   at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:355)
   at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324)
   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:285)
   at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)
   at android.support.v7.app.AlertController.installContent(AlertController.java:225)
   at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
   at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
   at android.app.Dialog.show(Dialog.java:314)
   at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
   at com.karanvir.search.MainActivity.onOptionsItemSelected(MainActivity.java:143)
   at android.app.Activity.onMenuItemSelected(Activity.java:3204)
   at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
   at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
   at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
   at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:113)
   at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
   at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:206)
   at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
   at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
   at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
   at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
   at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
   at android.widget.AdapterView.performItemClick(AdapterView.java:345)
   at android.widget.AbsListView.performItemClick(AbsListView.java:1547)
   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3821)
   at android.widget.AbsListView$3.run(AbsListView.java:5841)
   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:7224)
   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)


--------- beginning of system
07-11 20:44:43.388 752-13505/? E/android.os.Debug: ro.product_ship = true
07-11 20:44:43.388 752-13505/? E/android.os.Debug: ro.debug_level = 0x4f4c
07-11 20:44:43.388 752-13505/? E/android.os.Debug: sys.mobilecare.preload = false
07-11 20:44:43.428 13515-13515/? E/Zygote: v2
07-11 20:44:43.428 13515-13515/? E/Zygote: accessInfo : 0
07-11 20:44:49.228 28799-28830/? E/ContactsProvider_EventLog: Flush buffer to file cnt : 10 size : 7Kb duration : 153ms lastUpdatedAfter : 19910 ms mFlush_time_threasold : 2000 mCurrentSize : 3670

当我在设置中单击它的选项时,我的新警报对话框不会显示,而是在我的应用程序上完全崩溃。我对在这里做什么感到非常困惑,有人可以帮我吗?我也是开发的新手,如果这是一个新手问题,很抱歉。感谢您的阅读

安卓工作室

package com.karanvir.search;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random rn;
AutoCompleteTextView searchBar;
public static String urlGlobal;
TextView con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button=(Button) findViewById(R.id.button);
con=(TextView) findViewById(R.id.textView);
rn= new Random();
final SharedPreferences perf= this.getSharedPreferences("com.karanvir.shareprefernces", Context.MODE_PRIVATE);
//setting up an alert
if(perf==null){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("How to use")
.setMessage("Search what you want in the search bar! after we will choose a search engine our computer will recommend. After that you can change the search engine using the options menu in the right hand corner")
//then need to set a onclick listener with our positive button
.setPositiveButton("eng", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "its done", Toast.LENGTH_SHORT).show();
//where we would right our code where the user would confirm us to do
perf.edit().putString("choiceOne","eng").apply();
}
//25165012725
})
.setNegativeButton("fren", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
perf.edit().putString("choiceOne","fre").apply();
}
})
.show();
}


}
@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_settings4) {
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
return true;
} else if(id ==R.id.action_settings2){
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);
return true;
}else if (id==R.id.action_settings3){
Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentGoogle);
return true;
}else if(id==R.id.action_settings1){
new AlertDialog.Builder(getApplicationContext())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage(".....")
.show();
return true;
}
else if (id==R.id.action_settings5){
new AlertDialog.Builder(getApplicationContext())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage("This app intercha")
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public void jump(View view){
//intnet changing target of our code
urlGlobal=searchBar.getText().toString();
Log.i("stuff",urlGlobal);

int pageJump = rn.nextInt(3)+1;
if (pageJump==1){
//google
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
} else if (pageJump==2){
//YAHOO
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);
} else if(pageJump==3){
//GOOGLE
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
}
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
}
}

当你调用 AlertDialog.Builder(( 时,你也应该调用 .create(( 然后调用方法 dialog.show((

代码是这样的

AlertDialog dialog = new AlertDialog.Builder()
.setMessage("hello world")
.setPositiveButton("yes",new OnClickLisenter(Dialog dialog(something)))
.setXXX()
.create();
dialog.show()

把这个代码

public class MainActivity extends Activity {
Random rn;
AutoCompleteTextView searchBar;
public static String urlGlobal;
TextView con;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button=(Button) findViewById(R.id.button);
con=(TextView) findViewById(R.id.textView);
rn= new Random();
final SharedPreferences perf= this.getSharedPreferences("com.karanvir.shareprefernces", Context.MODE_PRIVATE);
//setting up an alert
if(perf==null){

new AlertDialog.Builder(this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("How to use")
.setMessage("Search what you want in the search bar! after we will choose a search engine our computer will recommend. After that you can change the search engine using the options menu in the right hand corner")
//then need to set a onclick listener with our positive button
.setPositiveButton("eng", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "its done", Toast.LENGTH_SHORT).show();
//where we would right our code where the user would confirm us to do
perf.edit().putString("choiceOne","eng").apply();

}
//25165012725

})
.setNegativeButton("fren", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
perf.edit().putString("choiceOne","fre").apply();

}
})
.show();
}




}
@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_settings4) {
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);

return true;
} else if(id ==R.id.action_settings2){
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);

return true;
}else if (id==R.id.action_settings3){
Intent intentGoogle= new Intent(getApplicationContext(),MainActivity.class);
startActivity(intentGoogle);

return true;
}else if(id==R.id.action_settings1){
new AlertDialog.Builder(getApplicationContext())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage(".....")
.show();
return true;
}
else if (id==R.id.action_settings5){
new AlertDialog.Builder(getApplicationContext())
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage("This app intercha")
.show();
return true;
}
return super.onOptionsItemSelected(item);
}

public void jump(View view){
//intnet changing target of our code
urlGlobal=searchBar.getText().toString();
Log.i("stuff",urlGlobal);


int pageJump = rn.nextInt(3)+1;
if (pageJump==1){
//google
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
} else if (pageJump==2){
//YAHOO
Intent intentGoogle= new Intent(getApplicationContext(),Yahoo.class);
startActivity(intentGoogle);
} else if(pageJump==3){
//GOOGLE
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intentGoogle);
}
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
}

}

在样式中.xml将样式更改为

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

相关内容

最新更新