我正在开发一款可执行以下操作的应用:
到目前为止,我认为我完成了大部分工作但是我已经陷入了如何修复程序的问题,所以它会按照我的意愿去做。
这是我的logcat
这是我的MainActivity:
package com.chatfriends;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button alexButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
alexButton = (Button) findViewById(R.id.button1); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
alexButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
alexbutton(arg0);
}
public void alexbutton(View v) {
Intent intent = new Intent(MainActivity.this, ActivityAlex.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
这是我的ActivityAlex:
package com.chatfriends;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityAlex extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private Button samButton; //initialize the buttons for "alex" and "sam"
public void initialize(){
samButton = (Button) findViewById(R.id.button2); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)
samButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
samButton(arg0);
}
public void samButton(View v) {
Intent intent = new Intent(ActivityAlex.this, MainActivity.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
startActivity(intent);
}
});
}
}
这是我的清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chatfriends"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>
<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=".ActivityAlex" />
</application>
</manifest>
以下是XML布局文件。
activity_main:
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button1"
android:onClick="startalexbutton" />
</RelativeLayout>
activityalex.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".ActivityAlex" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/userName2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textMultiLine" >
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button2"
android:onClick="startMainActivity" />
</RelativeLayout>
答案 0 :(得分:0)
android:onClick="startalexbutton"
这会告诉您的程序在按下方法时调用方法startalexbutton()
,此方法必须采用View
参数(public void startalexbutton(View view)
)。
此方法将在您的MainActivity
课程中进行;但是,由于您已经使用setOnClickListener()
,因此只需从XML中删除 android:onClick="startalexbutton"
。 (您也可以删除android:onClick="startMainActivity"
。)
PS:将来,请将您的LogCat日志放入文本格式;然后他们A)出现在搜索结果中,B)更容易阅读。
答案 1 :(得分:0)
您没有在initialize()
的{{1}}和onCreate
活动中致电ActivityAlex
来初始化按钮
MainActivity
并从xml中的两个按钮中删除onClick属性
在activity_main.xml中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize() ;//<<<<initialize here
}
在activityalex.xml中:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button1"
/>
注意:
当你为java代码中的任何View添加 <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button2"
/>
时,不需要在View xml中添加setOnClickListener
答案 2 :(得分:0)
这是因为您为xml中的每个按钮注册了按钮点击,并使用以下行。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button1"
android:onClick="startalexbutton" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_marginRight="20dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/button2"
android:onClick="startMainActivity" />
但是你还没有用这个名字定义任何onClick()方法。从xml中删除这些onclick行,或者使用相同的名称定义这些方法以监听按钮,而不是再次设置onClickListeners()。如果您决定使用在xml中注册的名称定义onClick方法,请执行以下操作。
public void startalexbutton(View view)
{
// Do what ever you need here when button is clicked. Because the control comes to this method when the button is clicked.
}
如果这样做,则不再需要initialize()方法。