我创建了一个名为SetIP.java的独立java类,只需一个简单的打印:
package com.myname.appname;
import android.util.Log;
public class SetIP {
public void hello(){
Log.d("System", "Hello World!");
}
}
在MainActivity中,我尝试通过以下方式调用它:
public class MainActivity extends AppCompatActivity {
SetIP setip = new SetIP();
setip.hello();
// OnCreate and Stuff
}
但错误表示无法解析符号'Hello'。请帮忙。感谢
答案 0 :(得分:2)
您需要在onCreate方法中调用它。
08:23:21,974 [-] [DataBridge-Core-pool-1-thread-2] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801943, data=[5, phone, g1, sell, samsung, iphone 4s, type, White 3 USB Port Car Charger Adapter For iPhone 4S 5S 5C 6 6S iPad Samsung Phone - Bid Now? sell, 27], isExpired=false} (Sanitized)
08:23:21,998 [-] [DataBridge-Core-pool-1-thread-4] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[11, phone, g1, buy, samsung, galaxy s4, type, #cellular #deals Samsung Galaxy S4 SCH-I545 16GB Verizon AT&T GSM UNLOCKED Cell Phone RF buy, 27], isExpired=false} (Sanitized)
08:23:22,030 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[13, phone, g1, sell, samsung, galaxy note, type, UNLOCKED T-Mobile Samsung Galaxy Note 3 SM-N900T 4G LTE GSM 32GB Smart Phone sell, 27], isExpired=false} (Sanitized)
08:23:22,031 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[18, phone, g1, sell, samsung, galaxy s6, type, Cell Phones : New Samsung Galaxy S6 Edge SM-G925F 5.1'' 16MP (FACTORY UNLOCKED) 32GB Phone sell, 27], isExpired=false} (Sanitized)
08:23:22,031 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[19, phone, g1, sell, samsung, galaxy s4, type, #cellular #deals Samsung i545 Galaxy S4 16GB Verizon 13MP Camera WiFi Cell Phone sell, 27], isExpired=false} (Sanitized)
08:23:22,031 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[1, phone, g1, sell, samsung, galaxy s6, type, Cell Phone USA : New Samsung Galaxy S6 Edge SM-G925F 5.1'' 16MP (FACTORY UNLOCKED) 32GB Ph? sell, 27], isExpired=false} (Sanitized)
08:23:22,033 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801944, data=[14, phone, g1, sell, samsung, galaxy note, type, Galaxy Note 6 will reportedly be the first Samsung phone to feature USB-C - sell, 27], isExpired=false} (Sanitized)
08:23:22,034 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801945, data=[16, phone, g1, buy, samsung, galaxy note, type, #unlocksquare Galaxy Note 6 will reportedly be the first Samsung phone to feature USB-C buy, 27], isExpired=false} (Sanitized)
08:23:22,035 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801945, data=[17, phone, g1, buy, samsung, galaxy s5, type, Cell Phone USA : NEW T-Mobile Protective Cover/Holster For Samsung Galaxy S5 Case Kickstan? buy, 27], isExpired=false} (Sanitized)
08:23:22,035 [-] [DataBridge-Core-pool-1-thread-5] INFO TenantId : -1234, Event Processor : R_H_Match, Event Stream : RelatedStream:1.0.0 (rs), before processing _Event{timestamp=1464144801945, data=[15, phone, g1, buy, samsung, galaxy s6, type, Cell Phone USA : Samsung Galaxy S6 SM-G920F 32GB Unlocked 16MP Smartphone #4422 buy, 27], isExpired=false} (Sanitized)
答案 1 :(得分:1)
当你现在正在调用时,你不应该动态调用任何类的对象的方法,正如你告诉你的// OnCreate and Stuff
低于对象的方法调用。
它必须是内部构造函数或一些方法,比如这里
SetIP setip = new SetIP();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setip.hello();
}
或强>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetIP setip = new SetIP();
setip.hello();
}
答案 2 :(得分:1)
您可以将SetIP实例化为MainActivity的全局变量,但调用该函数仍应驻留在MainActivity的函数中。一个例子是将其置于onCreate活动中,如下所示
public class MainActivity extends AppCompatActivity {
SetIP setip = new SetIP();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setip.hello();
}
// Other Stuff
}
答案 3 :(得分:1)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SetIP setip = new SetIP();
setip.hello();
}
// Other methods
}
答案 4 :(得分:0)
你需要改变只有方法是静态的
public class SetIP {
public static void hello(){
Log.d("System", "Hello World!");
}
}
尝试这项工作。