真正的菜鸟在这里。我正在尝试创建第二个活动来启动第二个布局。我尝试添加setOnClockListener的瞬间,启动应用程序进入黑暗屏幕,从未真正加载,然后崩溃。
有两种布局:activity_main.xml和activity_camcord.xml。我发布了MainActivty,第二个活动CamcordActivity和Manifest。
请让我知道我能做的其他任何事情,以便提供足够的信息
主要活动
package com.notebook.ksen;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Retrieve the button object
Button imageButtonCamCord = (Button)findViewById(R.id.imageButtonCamCord);
//Attach the Listener
imageButtonCamCord.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this , CamcordActivity.class);
startActivity(intent);
}
});
}
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);
setContentView(R.layout.activity_main);
return true;
}
}
SecondActivity
package com.notebook.ksen;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class CamcordActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camcord);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.camcord, menu);
setContentView(R.layout.activity_camcord);
return true;
}
}
清单
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.notebook.ksen.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 android:name=".CamcordActivity" android:label="camcord"/> </activity>
</application>
答案 0 :(得分:3)
如果在main.xml中使用ImageButton,请修复此问题;
主要活动
ImageButton imageButtonCamCord = (ImageButton )findViewById(R.id.imageButtonCamCord);
如果不起作用
主要活动
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);
//setContentView(R.layout.activity_main); ** don't use
return 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.camcord, menu);
//setContentView(R.layout.activity_camcord); ** don't use
return true;
}
答案 1 :(得分:2)
试试这个:
ImageButton imageButtonCamCord = (ImageButton)findViewById(R.id.imageButtonCamCord);
而不是:
Button imageButtonCamCord = (Button)findViewById(R.id.imageButtonCamCord);
如果没有解决问题,请尝试发布Logcat输出。