我正在Android Studio中学习Android开发。 我创建了一个应用程序,在点击按钮时显示一些文本和其他一些文本。 但它显示“遗憾的应用已停止” 另外,我只有一项活动。所以,我不可能忘记在AndroidManifest.xml中显示它 这是我的java代码:
package com.example.mahe.pro2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Button;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
private TextView tv = (TextView) findViewById(R.id.t);
private GestureDetectorCompat gd;
private Button bt = (Button) findViewById(R.id.b);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.gd = new GestureDetectorCompat(this,this);
bt.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
tv.setText("That was a Click!");
}
}
);
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
tv.setText("That was a Swipe, Baby");
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gd.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="YO!"
android:id="@+id/t"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/b"
android:layout_below="@+id/t"
android:layout_centerHorizontal="true"
android:layout_marginTop="164dp" />
答案 0 :(得分:2)
移动
apply
在t(apply(df, 1, function(x) {
w <- na.omit(which(is.na(x))[1:3])
x[w] <- list1[1:length(w)]
x }))
之后的TextView tv = (TextView) findViewById(R.id.t);
Button bt = (Button) findViewById(R.id.b);
下
答案 1 :(得分:1)
但是如果要将它们声明为全局变量,请使用
private TextView tv;
private Button bt
在onCreate方法之上,然后在onCreate方法中,在
之下的setContentView(R.layout.activity_main);
使用
tv = (TextView) findViewById(R.id.t);
bt = (Button) findViewById(R.id.b);