我无法使MenuItem正常工作,我之前创建了其他应用,但由于某种原因它只是没有出现:
MainActivity.java
package ie.example.artur.adminapp;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
EditText editTextName,editTextEmail,editTextPassword;
TextView textView;
private static final String DB_URL = "jdbc:mysql://10.3.2.51/socialmedia_website";
private static final String USER = "zzz";
private static final String PASS = "zzz";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView) findViewById(R.id.textView);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
//// try {
//// ViewConfiguration config = ViewConfiguration.get(this);
//// Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
//// if (menuKeyField != null) {
//// menuKeyField.setAccessible(true);
//// menuKeyField.setBoolean(config, false);
//// }
// } catch (Exception ignored) {
// }
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void btnConn(View view) {
Send objSend = new Send();
objSend.execute("");
}
private class Send extends AsyncTask<String, String, String>
{
String msg = "";
String name = editTextName.getText().toString();
String email = editTextEmail.getText().toString();
String password = editTextPassword.getText().toString();
@Override
protected void onPreExecute() {
textView.setText("Please Wait Inserting Data");
}
@Override
protected String doInBackground(String... strings) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
if (conn == null) {
msg = "Connection goes wrong";
} else {
String query = "Insert INTO users (name,email,password) VALUES('" + name+"','"+email+"','"+password+"')";
Statement stmt = conn.createStatement();
stmt.executeUpdate(query);
msg = "Inserting Successful!!";
}
conn.close();
}
catch(
Exception e
)
{
msg = "Connection goes Wrong";
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {textView.setText(msg);}
}
@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.
{
switch (item.getItemId())
{
case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
break;
}
return super.onOptionsItemSelected(item);
}}
}
menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="ie.example.artur.adminapp.MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="ifRoom|withText"/>
</menu>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ie.example.artur.adminapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我尝试过以下链接来修复我的问题,但没有帮助:
How to force use of overflow menu on devices with menu button
答案 0 :(得分:0)
在您的活动中添加此内容。
data: data1, data2, data3...
答案 1 :(得分:0)
我不知道您发布的MainActivity.java代码是否完整,无论如何看到的是缺少膨胀
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
其中menu_main.xml包含在res文件夹中正确创建的菜单文件夹中。