我有問題實施操作欄(我認爲現在被工具欄取代)。我想要的是一個有3個點的小下拉菜單,我可以有幾個選項,比如設置和點數。我試圖設置它,使菜單出現在homelistview.java頁面上。我覺得我一直在嘗試不同的解決方案,並且每次都會得到一個新的錯誤,並且總是指向下面提到的同一位置。實現工具欄調用空對象引用
setSupportActionBar(toolbar);
我原本以爲是因爲我沒有包含setContentView(R.layout.activity_main);但其中包括2個內容視圖並不令人滿意。任何幫助,將不勝感激。
這是我的代碼。
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
--------------- 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="cs495capstone.edu.bsu.myapplication.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_credits"
android:orderInCategory="100"
android:title="@string/action_credits"
app:showAsAction="never" />
<item
android:id="@+id/action_lovely"
android:orderInCategory="100"
android:title="@string/action_lovely"
app:showAsAction="never" />
</menu>
-------------- activity_main.xml中----------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="cs495capstone.edu.bsu.myapplication.HomeActivityListview">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/activity_add_appointment" />
<include layout="@layout/activity_add_history" />
<include layout="@layout/activity_appointments" />
<include layout="@layout/activity_dog_profile" />
<include layout="@layout/activity_history" />
<include layout="@layout/activity_home" />
<include layout="@layout/activity_home_activity_listview" />
<include layout="@layout/activity_login" />
<include layout="@layout/activity_new_dog" />
<include layout="@layout/activity_registration" />
<include layout="@layout/activity_splash_screen" />
<include layout="@layout/activity_update_dog" />
</android.support.design.widget.CoordinatorLayout>
----------------------- homeactivitylistview.xml ---------------------- ---
public class HomeActivityListview extends AppCompatActivity {
ListView lv;
Context context;
ArrayList dogName;
ArrayList dogID;
public static int [] dogImages={R.drawable.dogpic};
public static String [] dogNames={};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity_listview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, dogImages));
}
@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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendProfile(View view) {
Intent intent = new Intent(getApplicationContext(), DogProfileActivity.class);
startActivity(intent);
}
public void NumberOne(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number one");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void NumberTwo(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number two");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void AddNewDog(View view) {
Intent intent = new Intent(getApplicationContext(), NewDogActivity.class);
startActivity(intent);
}
}
------------------ main_activity.java -----------------
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;
}
@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);
}
}
-------------- homelistview.java ------------------
public class HomeActivityListview extends AppCompatActivity {
ListView lv;
Context context;
ArrayList dogName;
ArrayList dogID;
public static int [] dogImages={R.drawable.dogpic};
public static String [] dogNames={};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity_listview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, dogImages));
}
@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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendProfile(View view) {
Intent intent = new Intent(getApplicationContext(), DogProfileActivity.class);
startActivity(intent);
}
public void NumberOne(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number one");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void NumberTwo(View view) {
AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
alertDlg.setMessage("Confirm Dog went number two");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.setNegativeButton("Change", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//HomeActivity.super.onBackPressed();
}
});
alertDlg.create().show();
}
public void AddNewDog(View view) {
Intent intent = new Intent(getApplicationContext(), NewDogActivity.class);
startActivity(intent);
}
}
--- ------------ androidmanifest -----------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cs495capstone.edu.bsu.doggydid" >
<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/AppTheme" >
<activity android:name=".SplashScreenActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RegistrationActivity"
android:label="Registration" />
<activity
android:name=".NewDogActivity"
android:label="Dog Information" />
<activity
android:name=".LoginActivity"
android:label="Login" />
<activity
android:name=".HomeActivity"
android:label="@string/title_activity_home" />
<activity
android:name=".DogProfileActivity"
android:label="@string/title_activity_dog_profile" />
<activity
android:name=".HistoryActivity"
android:label="@string/title_activity_records" />
<activity
android:name=".addHistoryActivity"
android:label="@string/title_activity_add_record" />
<activity
android:name=".EventsActivity"
android:label="@string/title_activity_events" />
<activity
android:name=".addEventsActivity"
android:label="@string/title_activity_add_events" />
<activity
android:name=".UpdateDogActivity"
android:label="@string/title_activity_update_dog" />
<activity
android:name=".HomeActivityListview"
android:label="@string/title_activity_home_activity_listview"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
-----------------------錯誤讀數--------------------
04-14 13:35:36.905 3117-3117/cs495capstone.edu.bsu.doggydid E/AndroidRuntime:致命異常:主 工藝:cs495capstone.edu.bsu.doggydid,PID:3117 了java.lang.RuntimeException :無法啓動活動ComponentInfo {cs495capstone.edu.bsu.doggydid/cs495capstone.edu.bsu.doggydid.HomeActivityListview}:java.lang.NullPointerException:試圖調用虛擬方法'java.lang.CharSequence android.support.v7.widget .Toolbar.getTitle()'在空對象引用 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)在android.app.ActivityThread.access $ 800(ActivityThread.java:144) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) )在android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) 在java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method。(ZygoteInit.java:694) 由於: java.lang.NullPointerException:嘗試在android.support.v7.widget.ToolbarWidgetWrapper的空對象引用 上調用虛擬方法'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()'(ToolbarWidgetWrapper .java:98) at android.support.v7.widget.ToolbarWidgetWrapper。(ToolbarWidgetWrapper.java:91) at android.support.v7.app.ToolbarActionBar。(ToolbarActionBar.java:73) at android.support.v7 .app.AppCompatDelegateImplV7.setSupportActionBar(AppCo mpatDelegateImplV7.java:205) at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99) at cs495capstone.edu.bsu.doggydid.HomeActivityListview.onCreate(HomeActivityListview.java:31) at android。 app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app。 Activity.Thread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access $ 800(ActivityThread.java:144) at andro id.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
它看起來像你設置初始化'HomeActivityListview'和'MainActivity'中的'Toolbar'。您粘貼的XML文件看起來像是由'tools:context =「cs495capstone.edu.bsu.myapplication.HomeActivityListview」'判斷的'HomeActivityListview'的佈局文件。你可以驗證'activity_main.xml'包含一個''工具欄'與'ID'「R.id.toolbar」? – Ryan
這可能是它看着有一個工具欄作爲一個自定義的應用程序欄佈局和工具欄的新項目,然後看我的編輯XML沒有創建這些。當我試圖在設計視圖中添加它們時,它是固執的 – shadowice0823
是的,'設計'模式可能有點笨重。我建議在需要進行更改時使用XML文件的「文本」編輯模式。 – Ryan