我昨天刚刚开始学习Android,并希望编写一个非常简单的应用程序,其中包含2个用于学习目的的视图。
活动/查看1:1按钮和文本“Hello World” 单击该按钮后,它将转到下一个仅包含文本“testing”的活动/视图。
这是我的活动1的代码:
package helloworld.app;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem; import
android.support.v4.app.NavUtils;
/*import AudioRecordTest;*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void startRecording() {
setContentView(R.layout.next_page);
}
}
这是我的活动2的代码: 包helloworld.app;
import android.os.Bundle;
import android.app.Activity; import
android.view.Menu;
import android.view.MenuItem; import
android.support.v4.app.NavUtils;
/*import AudioRecordTest;*/
public class next_page extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next_page);
} }
这是我的活动1的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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginRight="40dp"
android:layout_marginTop="32dp"
android:text="Start Recording"
android:onClick="startRecording" />
</RelativeLayout>
编辑: 以下是来自logcat的错误消息
08-01 07:00:49.253:I / Choreographer(1326):跳过40帧!该 应用程序可能在其主线程上做了太多工作。
08-01 07:01:11.653:D / AndroidRuntime(1326):关闭虚拟机
08-01 07:01:11.653:W / dalvikvm(1326):threadid = 1:线程退出 未捕获的异常(组= 0x40a13300)
08-01 07:01:11.673:E / AndroidRuntime(1326):致命异常:主
08-01 07:01:11.673:E / AndroidRuntime(1326): java.lang.IllegalStateException:找不到方法 活动类helloworld.app.MainActivity中的startRecording(View) 对于带有id的视图类android.widget.Button上的onClick处理程序 '按钮1'
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.view.View $ 1.onClick(View.java:3578)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.view.View.performClick(View.java:4084)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.view.View $ PerformClick.run(View.java:16966)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.os.Handler.handleCallback(Handler.java:615)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.os.Looper.loop(Looper.java:137)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 07:01:11.673:E / AndroidRuntime(1326):at java.lang.reflect.Method.invokeNative(Native Method)
08-01 07:01:11.673:E / AndroidRuntime(1326):at java.lang.reflect.Method.invoke(Method.java:511)
08-01 07:01:11.673:E / AndroidRuntime(1326):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 07:01:11.673:E / AndroidRuntime(1326):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 07:01:11.673:E / AndroidRuntime(1326):at dalvik.system.NativeStart.main(原生方法)
08-01 07:01:11.673:E / AndroidRuntime(1326):引起: java.lang.NoSuchMethodException:startRecording [class android.view.View]
08-01 07:01:11.673:E / AndroidRuntime(1326):at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-01 07:01:11.673:E / AndroidRuntime(1326):at java.lang.Class.getMethod(Class.java:915)
08-01 07:01:11.673:E / AndroidRuntime(1326):at android.view.View $ 1.onClick(View.java:3571)
08-01 07:01:11.673:E / AndroidRuntime(1326):... 11更多
EDIT2: 我发现了自己的错误!我应该在View视图中传递startRecording函数。这是编辑过的代码:
public void startRecording(View view)
{
Intent intent = new Intent(this, next_page.class);
startActivity(intent);
}
答案 0 :(得分:3)
要使其正常运行,这应该是您的startRecording
方法:
public void startRecording(View v) {
// setContentView(R.layout.next_page); this will modify the current activity view
// if you want to start a new activity:
Intent i = new Intent(this, next_page.class);
startActivity(i);
}
确保您已在清单文件中声明了这两项活动。
要让android“看”并使用你的活动,必须在AndroidManifest.xml
文件中声明:
// ...
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".next_page" />
// ...
你应该从android开发者网站或一些教程中读到,这是基本的东西。
答案 1 :(得分:1)
您可以使用Intent类
从一个Activity转到Activitypublic void startRecording(View v) {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
答案 2 :(得分:0)
使用缩进在活动之间切换。 示例代码
btnNextScreen.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), SecondScreenActivity.class);
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("email", inputEmail.getText().toString());
Log.e("n", inputName.getText()+"."+ inputEmail.getText());
startActivity(nextScreen);
}
});
答案 3 :(得分:0)
嗯,你假设触发下一个Activity的是Button,我会这样做:
Button butt = (Button)findViewById(R.id.button1);
butt.setOnClickListener(new View.onClickListener(
public void onClick(View v) {
Intent intent = new Intent(v.getContext());
//optionally, you could provide it stuff to send to the second Activity if you wish
startActivity(intent);
}
));
但老实说,你可以通过研究自己解决这个问题。
答案 4 :(得分:0)
startRecording - 错误的代码。 需要写:
startActivity(new Intent(this, next_page.class));
学习如何格式化代码
答案 5 :(得分:0)
在第一个活动的onCreate方法中写如下
Button button1=(Button)findViewById(r.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
});