2014-03-31 146 views
0

我最近开始开发应用程序,我已经得到了我的第一次演出。这是一个简单的应用程序,带有一个徽标和一个链接到网站的按钮。每次我运行它,它崩溃了,我很难理解为什么,因为它是一个如此简单的程序。我花了很多时间在SO上寻找类似的问题,但无济于事。我也经历了eclipse并消除了任何编译器警告。有没有人有什么可能出错的想法?Android活动变黑,然后崩溃

错误消息:

我一直保留着一遍又一遍切东西出到应用程序的最简单的功能。它仍然无法工作。这是我的.java和主要活动。 (这是一个活动,一个类应用)

//necessary imports are omitted. I get no errors for imports 

public class FullscreenActivity extends Activity { 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fullscreen); 
    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      goToUrl("http://switchemup.com"); 
     } 
    }); 

} 

private void goToUrl(String url) { 
    Uri uriUrl = Uri.parse(url); 
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); 
    startActivity(launchBrowser); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.fullscreen, menu); 
    return true; 
} 
} 
<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".FullscreenActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button1" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/switchuplogo" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="94dp" 
    android:text="View Website" /> 

</RelativeLayout> 
+0

“并崩溃”有关崩溃类型logcat中的任何内容? –

+0

@StevenV我一直在使用模拟器时遇到问题,所以我只是建立了apk并将其安装在手机上。 – whrobbins

+0

您应该仍然可以使用adb通过apk连接到设备,并查看logcat。 [更多信息在文档中](http://developer.android.com/tools/help/logcat.html) –

回答

2

此行Button button = (Button) findViewById(R.id.button1);应该创建一个错误,这应该会导致应用程序崩溃你。您设置一个变量,其中包含应该在onCreate方法中的方法。

试试这个:

// init your variable only 
Button button; 

// Then in onCreate, as you already did: 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // ... 
    // set findViewById method only here 
    button = (Button) findViewById(R.id.button1); 
    // ... 
} 

让我知道,如果这有助于。