2015-01-11 168 views
0

我是新來的Android和開始跟隨tutorial了java.lang.RuntimeException:無法啓動活動ComponentInfo並延伸動作條

我的代碼包含以下文件:

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.lukas.tracker" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name"> 
     <meta-data 
      android:name="android.app.default_searchable" 
      android:value=".SearchResultsActivity" /> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 



    <!-- Location found activity --> 
    <activity 
     android:name=".LocationFound" 
     android:label="@string/activity_new_message" 
     android:parentActivityName=".MainActivity" > 
    </activity> 
</application> 

</manifest> 

LocationFound

package com.example.lukas.tracker; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.os.Bundle; 

public class LocationFound extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_location_found); 

     // get action bar 
     ActionBar actionBar = getActionBar(); 

     // Enabling Up/Back navigation 
     actionBar.setDisplayHomeAsUpEnabled(true); 
    } 
} 

MainActiv兩者均

package com.example.lukas.tracker; 


import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.Menu; 

import android.view.MenuItem; 



public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @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 boolean onOptionsItemSelected(MenuItem item) { 
     // Take appropriate action for each action item click 
     switch (item.getItemId()) { 
      case R.id.action_search: 
       // search action 
       return true; 
      case R.id.action_location_found: 
       // location found 
       LocationFound(); 
       return true; 
      case R.id.action_refresh: 
       // refresh 
       return true; 
      case R.id.action_help: 
       // help action 
       return true; 
      case R.id.action_check_updates: 
       // check for updates action 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    private void LocationFound() { 
     Intent i = new Intent(MainActivity.this, LocationFound.class); 
     startActivity(i); 
    } 

} 

activity_location_found.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp"> 

    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Location is found. Drop a Message here"/> 

</LinearLayout> 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

我有兩個問題。

  1. 如果我運行此代碼後,我的應用程序crashies點擊GPS圖標(位置找到)

enter image description here

,並拋出錯誤:

01-11 20:17:09.361 1893-1893/com.example.lukas.tracker E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.lukas.tracker, PID: 1893 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lukas.tracker/com.example.lukas.tracker.LocationFound}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.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) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
      at com.example.lukas.tracker.LocationFound.onCreate(LocationFound.java:18) 
      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.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.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) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

我認爲問題是在線

ActionBar actionBar = getActionBar(); 

actionBar爲null,但我不知道該如何更改以獲取對actionBar的實際引用。

  • 在教程用於

    公共類MainActivity延伸活動{

  • 但如果我跟着教程,有示出沒有操作欄,我將其更改爲

    public class MainActivity extends ActionBarActivity { 
    

    ,如果我是正確的它支持包的,它的使用應用程序的較低級別的API

    但是當我開始的項目,我選擇了分14 /靶21

    defaultConfig { 
         applicationId "com.example.lukas.tracker" 
         minSdkVersion 14 
         targetSdkVersion 21 
         versionCode 1 
         versionName "1.0" 
        } 
    

    那麼,爲什麼不只是extends Activity工作?

    感謝您的幫助

    編輯:

    enter image description here

    menu_main。XML:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:android="http://schemas.android.com/apk/res-auto" 
        xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> 
        <!-- Search/will display always --> 
        <item android:id="@+id/action_search" 
         android:icon="@drawable/ic_action_search" 
         android:title="@string/action_search" 
         android:showAsAction="ifRoom"/> 
    
        <!-- Location Found --> 
        <item android:id="@+id/action_location_found" 
         android:icon="@drawable/ic_action_location_found" 
         android:title="@string/action_location_found" 
         android:showAsAction="ifRoom" /> 
    
        <!-- Refresh --> 
        <item android:id="@+id/action_refresh" 
         android:icon="@drawable/ic_action_refresh" 
         android:title="@string/action_refresh" 
         android:showAsAction="ifRoom" /> 
    
        <!-- Help --> 
        <item android:id="@+id/action_help" 
         android:icon="@drawable/ic_action_help" 
         android:title="@string/action_help" 
         android:showAsAction="never"/> 
    
        <!-- Check updates --> 
        <item android:id="@+id/action_check_updates" 
         android:icon="@drawable/ic_action_refresh" 
         android:title="@string/action_check_updates" 
         android:showAsAction="never" /> 
    </menu> 
    

    回答

    2

    你MainActivity擴展ActionBarActivity(從支持包的活動),這意味着你的應用程序使用程序兼容性主題。

    由於您的minSdk是14,您根本不應該使用支持包。

    1. 使您的活動延伸Activity

    2. 然後在你的styles.xml有你AppTheme是指一個新的主題,如:

      <style name="AppTheme" parent="android:Theme.Holo"> 
          <!-- Customize your theme here. --> 
      </style> 
      
    +0

    THX,這種風格的作品,現在它運行,也當我選擇的菜單項,它會打開新窗口,但圖標從頂欄中消失,請參閱我的主帖子中的編輯以獲取打印屏幕。你能告訴我現在問題在哪裏嗎? – sala

    +0

    @ sala84從現在開始你沒有使用支持包,你不應該使用自定義名稱空間'app'。所以只要改成'android:showAsAction',你應該沒問題。 – Simas

    +0

    我改變了應用程序 - > android,但現在我得到錯誤在線: sala

    相關問題