2013-11-27 209 views
-1

我看過其他Q和答案,但找不到我的代碼有什麼問題嗎? 我一定在做錯事,所以如果有人能幫助我,那將是非常感謝。 我檢查了第一頁和第二頁以及清單。Android應用程序不斷崩潰按鈕點擊

package com.example.fmf; 

import com.example.fmf.Search; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.view.View.OnClickListener; 


public class MainActivity extends Activity { 






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

     Button button = (Button) findViewById(R.id.go_button); 

     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
        goMenu(); 
      } 

     }); 
    } 


    public void goMenu() 
    { 
     Intent myIntent = new Intent(this, Search.class); 
     startActivity(myIntent); 
    } 


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

} 

第2頁

package com.example.fmf; 


import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.Spinner; 
import android.support.v4.app.NavUtils; 
import android.annotation.TargetApi; 
import android.content.Intent; 
import android.os.Build; 

public class Search extends Activity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search); 
    // Show the Up button in the action bar. 
    setupActionBar(); 
    } 

    /** 
    * Set up the {@link android.app.ActionBar}, if the API is available. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // This ID represents the Home or Up button. In the case of this 
     // activity, the Up button is shown. Use NavUtils to allow users 
     // to navigate up one level in the application structure. For 
     // more details, see the Navigation pattern on Android Design: 
     // 
     // http://developer.android.com/design/patterns/navigation.html#up-  vs-back 
     // 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

Bundle extras = getIntent().getExtras(); 
public final String ID_CITY = extras.getString("CITY_TH"); 

//retrieve list view value selected and store in variable restaurant 
//Not sure how to do this... 



} 

清單

<uses-sdk 
     android:minSdkVersion="8" 
     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" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".Search" 
     android:label="@string/app_name" 
     android:parentActivityName=".MainActivity" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".MainActivity" /> 
    </activity> 
    <activity 
     android:name=".Info" 
     android:label="@string/app_name" 
     android:parentActivityName=".Search" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".Search" /> 
     </activity> 
    </application> 

</manifest> 

抱歉耽擱日誌貓(不知道如何發佈這一切)

11-27 15:40:05.118: D/AndroidRuntime(771): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
11-27 15:40:05.128: D/AndroidRuntime(771): CheckJNI is ON 
11-27 15:40:05.158: D/dalvikvm(771): Trying to load lib libjavacore.so 0x0 
11-27 15:40:05.168: D/dalvikvm(771): Added shared lib libjavacore.so 0x0 
11-27 15:40:05.198: D/dalvikvm(771): Trying to load lib libnativehelper.so 0x0 
11-27 15:40:05.198: D/dalvikvm(771): Added shared lib libnativehelper.so 0x0 
11-27 15:40:05.278: E/cutils-trace(771): Error opening trace file: No such file or directory (2) 
11-27 15:40:05.868: D/AlertService(549): Beginning updateAlertNotification 
11-27 15:40:05.898: D/AndroidRuntime(771): Calling main entry com.android.commands.am.Am 
11-27 15:40:05.918: D/dalvikvm(771): Note: class Landroid/app/ActivityManagerNative; has 163 unimplemented (abstract) methods 
11-27 15:40:05.948: I/ActivityManager(286): Force stopping package com.example.fmf appid=10048 user=-1 
11-27 15:40:05.959: I/ActivityManager(286): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.fmf/.MainActivity} from pid 771 
11-27 15:40:06.328: I/WindowManager(286): Screenshot max retries 4 of Token{419849f0 ActivityRecord{41984880 u0 com.android.launcher/com.android.launcher2.Launcher}} appWin=Window{419646b0 u0 com.android.launcher/com.android.launcher2.Launcher} drawState=4 
11-27 15:40:06.328: W/WindowManager(286): Screenshot failure taking screenshot for (480x800) to layer 21000 
11-27 15:40:06.338: D/AndroidRuntime(771): Shutting down VM 
11-27 15:40:06.348: D/dalvikvm(771): GC_CONCURRENT freed 92K, 16% free 513K/608K, paused 1ms+0ms, total 11ms 
11-27 15:40:06.358: D/AlertService(549): No fired or scheduled alerts 
11-27 15:40:06.388: W/dalvikvm(783): PR_CAPBSET_DROP 0 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled. 
11-27 15:40:06.388: W/dalvikvm(783): PR_CAPBSET_DROP 1 failed: Invalid argument. Please make sure your kernel is compiled with file capabilities support enabled. 
+2

請發佈堆棧跟蹤 – Emmanuel

+0

發佈'LogCat' – Rick

+0

查找了跟蹤文件錯誤,但發現它通常是由'setContentView(R.layout.activity_main)'引起的;'在錯誤的地方,但我很確定它在適合我的地方? – user3028988

回答

2

沒有堆棧跟蹤可能很難說。從我看到你有兩個可能的罪魁禍首。

首先

Bundle extras = getIntent().getExtras(); 
public final String ID_CITY = extras.getString("CITY_TH"); 

在你的搜索類,你正試圖從包中獲取數據。但是在你的MainActivity.goMenu()中,你沒有傳入一個包。這可能會導致NULLPOINTEREXCEPTION

setupActionBar(); 

請嘗試評論說出來你Search.class和看到的結果是什麼。我無法從清單中看到您的主題。所以你甚至可能沒有可能導致崩潰的操作欄。所以,如果註釋掉上述工程線路,以下內容添加到您的清單:

變化:

android:theme="@style/AppTheme" 

要:

android:theme="@android:style/Theme.Holo" 

,看看是否可行。

+0

打敗我吧。絕對值得評論這兩行,看看問題是否消失。 – Squonk

+1

此外,一旦這些工作,他們應該被移動像一個方法像'onCreate()' – codeMagic

+0

我一直在試圖將清單更改爲'android:theme =「@ style/Theme.Holo」'但我保持發現沒有找到資源的錯誤? @Brianjs – user3028988

0

的問題應該是這樣的:

button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
       goMenu(); 
     } 

    }); 
} 


public void goMenu() 
{ 
    Intent myIntent = new Intent(this, Search.class); 
    startActivity(myIntent); 
} 

剛剛嘗試替換此:

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent myIntent = new Intent(MainActivity.this,Search.class); 
      MainActivity.this.startActivity(myIntent); 

     } 
    }); 

希望它可以幫助你。

+0

謝謝,但我最初有你的建議,並將其更改爲當前的一個試圖解決問題,所以我不認爲這是它(雖然你的可能更好)。 @ user2675569 – user3028988

相關問題