1

我有一个标签解析错误,我一直试图解决这个错误,但一切都是徒劳的,我想将数据从一个活动移动到另一个活动,但我收到了这个错误:

------>“标签无法解析为变量”

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);
    }  
});

更新代码:

登录活动1.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);
}

设置.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>

设置.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");   
   }
4

4 回答 4

2

在 Activity 的 onCreate 之前将 TAG 声明为 Activity 级别字段为:

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());
于 2012-12-27T18:26:08.613 回答
0

TAG应该在某处声明。因为都是大写,我怀疑这是一个

public static final String TAG = "MyActivity";

如果您打算使用该变量,请务必声明并实例化该变量。

于 2012-12-27T18:26:35.780 回答
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);
    }
});
于 2012-12-27T18:26:47.663 回答
0

声明字符串 TAG = "activityName"; 在使用 TAG 变量之前,您可以删除此行,因为它与您的功能无关。现在要获取您通过意图传递给下一个活动的数据,您可以在下一个活动的 onCreate() 方法中执行此操作

String name = getIntent().getStringExtra("NAME", "");
于 2012-12-27T18:27:30.093 回答