2013-11-28 34 views
0

這裏是代碼,第29行對應的bextras按鈕配置,它似乎是正確的。 第29行 - extras.setTypeface(font3);當我刪除它並啓動應用程序時,它會轉到extras.setOnClickListener並在那裏顯示錯誤,當我刪除整個額外按鈕及其道具(包括setOnClickListener)時,該應用程序正常工作。應用程序失敗,logcat說行29似乎沒問題

怎麼了?

public class TheBeginning extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_the_beginning); 
    Button gogym = (Button) findViewById(R.id.bgym); 
    Typeface font = Typeface.createFromAsset(getAssets(), "Sanvito.ttf"); 
    gogym.setTypeface(font); 
    Button gofav = (Button) findViewById(R.id.bfav); 
    Typeface font1 = Typeface.createFromAsset(getAssets(), "Sanvito.ttf"); 
    gofav.setTypeface(font1); 
    Button goindex = (Button) findViewById(R.id.bindex); 
    Typeface font2 = Typeface.createFromAsset(getAssets(), "Sanvito.ttf"); 
    goindex.setTypeface(font2); 
    Button extras = (Button) findViewById(R.id.bextra1); 
    Typeface font3 = Typeface.createFromAsset(getAssets(), "Sanvito.ttf"); 
    **extras.setTypeface(font3);** //Line 29 
    TextView minit = (TextView) findViewById(R.id.minititle); 
    Typeface font4 = Typeface.createFromAsset(getAssets(), "Alba.TTF"); 
    minit.setTypeface(font4); 

    gogym.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent gogymint = new  Intent("com.irespekt.gymbook.GOTOgym"); 
      startActivity(gogymint); 
     } 
    }); 
    gofav.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent gofavint = new Intent("com.irespekt.gymbook.GOTOFAV"); 
      startActivity(gofavint); 
     } 
    }); 
    goindex.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent goindexint = new Intent(
         "com.irespekt.gymbook.INDEXPAGEACTIVITY"); 
      startActivity(goindexint); 
     } 
    }); 
    extras.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent extrasint = new Intent(
        "com.irespekt.gymbook.PICTUREVIEWFLIP"); 
      startActivity(extrasint); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.the_beginning, menu); 
    return true; 
} 

} 

編輯

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.irespekt.gymbook.TheBeginning" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.irespekt.gymbook.GoToGym" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.irespekt.gymbook.GOTOGYM" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.irespekt.gymbook.GoToFav" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.irespekt.gymbook.GOTOFAV" /> 






      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.irespekt.gymbook.IndexPageActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.irespekt.gymbook.INDEXPAGEACTIVITY" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
      <activity 
     android:name="com.irespekt.gymbook.WebViewActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.irespekt.gymbook.WEBVIEWACTIVITY" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.irespekt.gymbook.PictureViewFlip" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.irespekt.gymbook.PICTUREVIEWFLIP" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

佈局XML

<LinearLayout 
    android:id="@+id/LinearLayout04" 
    android:layout_width="fill_parent" 
    android:layout_height="186dp" 
    android:layout_weight="16.78" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/bgym" 
     style="@style/btnStylePaprika" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="15.0dip" 
     android:text="Go To Gym" 
     android:textSize="20.0sp" /> 

    <Button 
     android:id="@+id/bfav" 
     style="@style/btnStylePaprika" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="5.0dip" 
     android:text="Favorites" 
     android:textSize="20.0sp" /> 

    <Button 
     android:id="@+id/bindex" 
     style="@style/btnStylePaprika" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="5.0dip" 
     android:text="Index" 
     android:textSize="20.0sp" /> 

    <Button 
     android:id="@+id/bextra1" 
     style="@style/btnStylePaprika" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="5.0dip" 
     android:text="Extras" 
     android:textSize="20.0sp" /> 
</LinearLayout> 

logcat的

11-28 12:35:42.598: D/dalvikvm(875): GC_FOR_ALLOC freed 69K, 6% free 2589K/2732K, paused 243ms, total 257ms 
11-28 12:35:42.658: I/dalvikvm-heap(875): Grow heap (frag case) to 4.638MB for 2139636-byte allocation 
11-28 12:35:42.761: D/dalvikvm(875): GC_FOR_ALLOC freed 2K, 4% free 4676K/4824K, paused 106ms, total 106ms 
11-28 12:35:44.048: D/AndroidRuntime(875): Shutting down VM 
11-28 12:35:44.048: W/dalvikvm(875): threadid=1: thread exiting with uncaught exception (group=0x414c4700) 
11-28 12:35:44.148: E/AndroidRuntime(875): FATAL EXCEPTION: main 
11-28 12:35:44.148: E/AndroidRuntime(875): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.irespekt.gymbook/com.irespekt.gymbook.TheBeginning}: java.lang.NullPointerException 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.os.Looper.loop(Looper.java:137) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread.main(ActivityThread.java:5103) 
11-28 12:35:44.148: E/AndroidRuntime(875): at java.lang.reflect.Method.invokeNative(Native Method) 
11-28 12:35:44.148: E/AndroidRuntime(875): at java.lang.reflect.Method.invoke(Method.java:525) 
11-28 12:35:44.148: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
11-28 12:35:44.148: E/AndroidRuntime(875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-28 12:35:44.148: E/AndroidRuntime(875): at dalvik.system.NativeStart.main(Native Method) 
11-28 12:35:44.148: E/AndroidRuntime(875): Caused by: java.lang.NullPointerException 
11-28 12:35:44.148: E/AndroidRuntime(875): at com.irespekt.gymbook.TheBeginning.onCreate(TheBeginning.java:29) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.Activity.performCreate(Activity.java:5133) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-28 12:35:44.148: E/AndroidRuntime(875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
11-28 12:35:44.148: E/AndroidRuntime(875): ... 11 more 
11-28 12:36:09.321: I/Process(875): Sending signal. PID: 875 SIG: 9 
+0

您可以提供完整的LogCat以及定義了bextra1的佈局xml嗎? – Grambot

+0

請檢查我的編輯 –

+0

@TheCapn請檢查我的編輯,它包含您所需要的一切,你是否能夠找到任何東西? –

回答

0

MyBad ......對不起,我發現的錯誤。

帶有id - bextra的按鈕的id爲gonow,現在我改變了activity_thebeginning.xml佈局中的id,並且FORGOT重命名了其餘的xlarge,xland等,導致了錯誤。

對不起,如果我給任何人頭痛。感謝壽!

0

首先,你必須保持你的資產文件夾項目中,而不是內部

src然後,創建一個名爲字體內資產的文件夾。然後,將特定的字體字體ttf文件放入其中。您可以在編碼中使用字體字體,例如:

字體font = Typeface.createFromAsset(getAssets(),「fonts/Sanvito.ttf」); gogym.setTypeface(font);

有問題在下面的語句

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Sanvito.ttf"); 
+0

但是第一個以上的所有其他按鈕都運行良好,事實上,extras按鈕和字體運行良好,我不知道我做了什麼破壞了配置,我知道錯誤在創建新活動時開始PictureFlipActivity在添加到清單並且應用程序已運行後發生錯誤。麻煩 –

相關問題