我有一个标签解决错误,我试图解决这个问题,但都是徒劳的, 我想将数据从一个活动移动到另一个活动,但我收到此错误:
------> “标签无法解析为变量”
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(), home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
Log.d(TAG, name);
}
});
更新后的代码:
LoginActivity1.java
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.trekeyes.android.CustomHttpClient;
public class LoginActivity1 extends Activity {
Button btnLinkToRegistrScrn;
Button loginbtn1;
Button cancelBtn;
TextView error;
EditText LgnTxt, LgnPsw;
public static DefaultHttpClient httpClient;
public static String TAG="LoginActivity1";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
LgnTxt = (EditText) findViewById(R.id.editTextLgnEmail);
LgnPsw = (EditText) findViewById(R.id.editTextLgnPassword);
error = (TextView)findViewById(R.id.login_error1);
addListenerOnButton();
// Link to Register Screen
btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen);
btnLinkToRegistrScrn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
Intent i = new Intent(v.getContext(), Activity.class);
startActivity(i);
}});
// Link to Cancel Button
cancelBtn = (Button) findViewById(R.id.btnCancelLgn);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
// Intent i = new Intent(v.getContext(), PageApp.class);
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(i);
}
});
}
public void addListenerOnButton() {
loginbtn1 = (Button) findViewById(R.id.btnLogin);
loginbtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
///////// Database Handling
// TODO Auto-generated method stub
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("email", LgnTxt.getText().toString()));
postParameters.add(new BasicNameValuePair("password", LgnPsw.getText().toString()));
String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://10.0.2.2/db/lgn.php", postParameters);
String res=response.toString();
res= res.replaceAll("\\s+","");
if(res.equals("1"))
{
error.setText("Correct Email And Password");
Intent intent = new Intent(arg0.getContext(), boardAct.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EMAIL", LgnTxt.getText().toString());
startActivity(intent);
Log.d(TAG,LgnTxt.getText().toString());
finish();
}
else
error.setText("Sorry!! Incorrect Email or Password");
} catch (Exception e) {
LgnTxt.setText(e.toString());
}
}
});
}
}
boardAct.java
public class boardAct extends MapActivity {
public static String TAG="boardAct";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
}
的settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginTop="0dp"
android:text="@string/accountSettings"
android:textSize="25sp"
android:layout_gravity="center"
android:textStyle="bold" />
<TextView
android:id="@+id/txtlabellogSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginTop="6dp"
android:state_enabled="false"
android:textSize="15sp"
android:text="@string/labelsettings"
android:layout_gravity="center"
android:textStyle="bold" />
<TextView
android:id="@+id/txtboxlogSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginTop="6dp"
android:state_enabled="false"
android:textSize="15sp"
android:layout_gravity="center"
android:textStyle="bold" />
</LinearLayout>
Setting.java
public class AccSettingsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
///////////// For TxtlogSetting ////////////
TextView logText = (TextView) findViewById(R.id.txtboxlogSetting);
String email = getIntent().getStringExtra("EMAIL");
logText.setText("email");
}
答案 0 :(得分:2)
在onCreate of Activity之前将TAG声明为活动级别字段:
public class Your_Main_Activity extends Activity {
public static String TAG="YOUR_ACTIVITY_NAME";
Button ok;
@Override
public void onCreate(Bundle savedInstanceState) {
ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(Your_Main_Activity.this, home.class);
newActivity.putExtra("NAME", name.toString());
startActivity(newActivity);
Log.d(TAG, name.toString());
}
});
// your code here
}
}
如果您已将其在其他活动中声明为静态,则将其作为:
访问Log.d(Your_Other_Activity.TAG, name.toString());
答案 1 :(得分:0)
TAG
。因为它是全部大写,我怀疑它是
public static final String TAG = "MyActivity";
如果您打算使用该变量,请务必声明并实例化该变量。
答案 2 :(得分:0)
public static final String TAG = "NAME"
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(),home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
Log.d(TAG,name);
}
});
答案 3 :(得分:0)
声明String TAG =“activityName”;在使用TAG变量之前 您可以删除此行以及它与您的功能无关。现在,为了获得您通过意图传递给下一个活动的数据,您可以在下一个活动的onCreate()方法中执行此操作
String name = getIntent().getStringExtra("NAME", "");