我正在尝试构建一个文本视图,该文本视图转到onClick但它不起作用,他们告诉我添加这个代码我做了但是我有很多错误。
这是我的MainActivity.java
package imamalsajadsayings.android.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void runNextTask(){
final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
final TrackerInfo newInfo = new TrackerInfo();
//set up for model selection
TextView modelTextview = (TextView)addView.findViewById(R.id.state1);
modelTextview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
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);
return true;
}
}
是我的textview
<TextView
android:id="@+id/state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tracker_model"
android:clickable="true"
注意:我没有创建addnewtracker
错误:
Description Resource Path Location Type
TrackerInfo cannot be resolved to a type MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 20 Java Problem
addnewtracker cannot be resolved or is not a field MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 19 Java Problem
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){}) MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 23 Java Problem
TrackerInfo cannot be resolved to a type MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 20 Java Problem
The method onClick(View) of type new OnClickListener(){} must override or implement a supertype method MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 25 Java Problem
OnClickListener cannot be resolved to a type MainActivity.java /ImamAlsajadsayings/src/imamalsajadsayings/android/com line 23 Java Problem
答案 0 :(得分:0)
添加到活动:
public void modelTextViewClick(View v) {
//do something on click
}
和布局:
<TextView android:id="@+id/state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tracker_model"
android:clickable="true"
android:onClick="modelTextViewClick"/>
并删除您的on click侦听器。
答案 1 :(得分:0)
将以下代码更改为XML文件
<TextView android:id="@+id/state1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tracker_model"
android:clickable="true"
android:onClick="runNextTask"/>
只需修改您的java文件,如下所示。
public void runNextTask(View v){
final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
final TrackerInfo newInfo = new TrackerInfo();
//set up for model selection
TextView modelTextview = (TextView)addView.findViewById(R.id.state1);
modelTextview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
答案 2 :(得分:0)
您需要导入此包以避免此错误
错过了这个 - &gt; import android.view.View.OnClickListener;
答案 3 :(得分:0)
这就是为什么它不起作用
应该是
package imamalsajadsayings.android.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private LayoutInflater inflater;
private LinearLayout someLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
someLayout = (LinearLayout) findViewById(R.id.some_layout); //layout present in activity_main
inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
runNextTask();
}
public void runNextTask(){
LinearLayout mInflatedLayout = (LinearLayout) inflater.inflate(R.layout.addnewtracker, null);
final TrackerInfo newInfo = new TrackerInfo();
//set up for model selection
TextView modelTextview = (TextView)mInflatedLayout.findViewById(R.id.state1);
someLayout.addView(mInflatedLayout);
modelTextview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
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);
return true;
}
}
假设你在 activity_main 布局中使用 LinearLayout
答案 4 :(得分:0)
使用下面的代码。
package imamalsajadsayings.android.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void runNextTask(){
final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
final TrackerInfo newInfo = new TrackerInfo();
//set up for model selection
TextView modelTextview = (TextView)addView.findViewById(R.id.state1);
}
@Override
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);
return true;
}
public void modelTextViewClick(View view)
{
// here view reference is your text view reference.
// put your on click handler code.
}
}
此代码与您的代码之间的差异是onclick处理程序。在xml中,您已经定义了一个必须在活动代码中使用的单击处理程序。其他方式设置onclick侦听器处理程序敌人的小部件。你同时做两个都不允许在android中的任何小部件。