Hareket çubuğunu uygularken (şimdi araç çubuğuyla değiştirildiğini düşünüyorum) sorun yaşıyorum. İstediğim, ayarları ve kredileri gibi birkaç seçeneğim olduğu 3 nokta ile açılan küçük bir menü. Menüyü homelistview.java sayfasında görünecek şekilde ayarlamaya çalıştım. Farklı çözümler deneyen ve her seferinde yeni bir hata alıp götürdüğümü hissettim ve her zaman aşağıda belirtilen aynı yere geri dönüyorum.Null object reference çağıran araç çubuğu uygulaması
setSupportActionBar(toolbar);
Başlangıçta ben setContentView (R.layout.activity_main) içermiyordu çünkü olduğunu düşünmüştü; ama 2 içerik görüşü de dahil olmaktan mutlu değildi. Herhangi bir yardım takdir edilecektir.
İşte kodum.
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>
----------------------- Hata Okuma --------------------
04-14 13: 35: 36,905 3117-3117/cs495capstone.edu.bsu.doggydid E/AndroidRuntime: önemli özel durum: ana Proses: cs495capstone.edu.bsu.doggydid PID: 3117 java.lang.RuntimeException : Etkinleştirilemiyor ComponentInfo {cs495capstone.edu.bsu.doggydid/cs495capstone.edu.bsu.doggydid.HomeActivityListview}: java.lang.NullPointerException: sanal yöntemi 'java.lang.CharSequence android.support.v7.widget çağırmayı deneyin. .Toolbar.getTitle() 'null object reference üzerinde android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2298)Android.app.ActivityThread.handleLaunchActivity adresinde(ActivityThread.java:2360) android.app.ActivityThread.access $ 800 (ActivityThread.java:144) adresinden , android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1278) adresinde Android.os.Looper.loop (Looper.java:135) 'de , android.app.ActivityThread.main (ActivityThread.java:5221) java.lang.reflect.Method.invoke (Yerel Yöntem) adresinde java.lang.reflect.Method.invoke (Yöntem.java: 372) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694) Nedeni: java.lang.NullPointerException: android.support.v7.widget.ToolbarWidgetWrapper adresindeki boş nesne başvurusu üzerindeki "java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle() 'nı ( ) çağırmayı deneyin. (ToolbarWidgetWrapper) .java: 98) android.support.v7.widget.ToolbarWidgetWrapper adresindeki . (ToolbarWidgetWrapper.java:91) at android.support.v7.app.ToolbarActionBar. (ToolbarActionBar.java:73) android.support.v7 adresinde .app.AppCompatDelegateImplV7.setSupportActionBar (AppCo cs495capstone.edu.bsu.doggydid.HomeActivityListview.onCreate (HomeActivityListview.java:31) robot en de android.support.v7.app.AppCompatActivity.setSupportActionBar (AppCompatActivity.java:99) de mpatDelegateImplV7.java:205) . android.app adresindeki android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105) adresindeki android.app adresindeki app.Activity.performCreate (Activity.java:5933) ( ). ActivityThread.handleLaunchActivity (ActivityThread.java:2360) at android.app.ActivityThread.access $ 800 (ActivityThread.java:144) andro'da android.os.Looper.loop (Looper.java:135) adresindeki android.os.Handler.dispatchMessage (Handler.java:102) adresinden id.app.ActivityThread $ H.handleMessage (ActivityThread.java:1278) java.lang.reflect.Method.invoke (Yerel Yöntem) at java.lang.reflect.Method.invoke (Yöntem.java:372) at android.app.ActivityThread.main (ActivityThread.java:5221) com.android.internal.os.ZygoteInit.main de .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) (ZygoteInit.java:694)
bunu başlatmak ... dolayısıyla kapsamı o yöntemin dışına gitti, sadece onCreate başlatılmış Araç Çubuğu hem "HomeActivityListview" hem de "MainActivity". Yapıştırdığınız XML dosyası, "HomeActivityListview" için bu satırın yargılanacağı düzen dosyası gibi görünüyor: tools: context = "cs495capstone.edu.bsu.myapplication.HomeActivityListview" '. Activity_main.xml ifadesinin "R.id.toolbar" kimliğine sahip bir "Araç Çubuğu" içerdiğini doğrulayabilir misiniz? – Ryan
Bu, özel bir uygulama çubuğu düzeni ve araç çubuğu olarak bir araç çubuğuna sahip olan yeni projeye bakabilir, daha sonra xml'yi düzenleme işlemine bakma, bunları oluşturmadı. ve tasarım görünümünde eklemeye çalıştığımda inatçı olmanın – shadowice0823
Evet, 'Tasarım' modu biraz tıkalı olabilir. Bir değişiklik yapmanız gerektiğinde, XML dosyaları için 'Metin' düzenleme modunu kullanmanızı tavsiye ederim. – Ryan